简体   繁体   中英

Abstract Class and Its Subclass

I have the following codes which I found on the internet, this might be a simple question but your explanations will help me a lot.

This is the abstract class named SongComponent

public abstract class SongComponent {

    public void add(SongComponent newSongComponent){

        throw new UnsupportedOperationException();

    }
    public void remove(SongComponent newSongComponent){

        throw new UnsupportedOperationException();

    }

    public SongComponent getComponent(int componentIndex){

        throw new UnsupportedOperationException();

    }

    public String getSongName(){

        throw new UnsupportedOperationException();

    }

    public String getBandName(){

        throw new UnsupportedOperationException();

    }

    public int getReleaseYear(){

        throw new UnsupportedOperationException();

    }

    public void displaySongInfo(){

        throw new UnsupportedOperationException();

    }




}

This is the class that extends the abstract class named SongGroup

import java.util.ArrayList;
import java.util.Iterator;
public class SongGroup extends SongComponent {

    ArrayList songComponents = new ArrayList();

    String groupName;
    String groupDescription;

    public SongGroup(String newGroupName, String newGroupDesc){

        groupName = newGroupName;
        groupDescription = newGroupDesc;


    }

    public String getGroupName(){
        return groupName;
    }

    public String getGroupDescription(){
        return groupDescription;
    }

    // It overrides the add() method from its superclass 
    public void add(SongComponent newSongComp){
        songComponents.add(newSongComp);
    }

    public void remove(SongComponent newSongComp){
        songComponents.remove(newSongComp);
    }

    public SongComponent getComponent(int compIndex){
        return (SongComponent)songComponents.get(compIndex);
    }

    public void displaySongInfo(){

        System.out.println( getGroupName() + " " + 
        getGroupDescription());

        Iterator songIterator = songComponents.iterator();

        while(songIterator.hasNext()){
            SongComponent songInfo = (SongComponent)songIterator.next();
            songInfo.displaySongInfo();
        }
    }


}

My question is, in what situation, the method of add() and other methods which have been overridden of the abstract class will be called? For example, when the method add() of the abstract class that results in throwing new UnsupportedOperationException will be called?

SongComponent may have other concrete sub-classes (other than SongGroup ) that don't override add() . For such sub-classes, calling add() would execute the base class implementation and throw the UnsupportedOperationException exception.

An alternative implementation of SongComponent would be to make all the methods that currently throw UnsupportedOperationException abstract, which would force any concrete sub-class of SongComponent to override them.

You may call one of the overridden classes in your subclass eg by calling super.add() . As this will throw an UnsupportedOperationException I would not recommend doing that.

If you have another subclass, eg

 class SongWithNoComponents extends SongComponent{
      ...
   }

And you don't override add() there, an UnsupportedOperationException is thrown, which would make sense in this scenario, as this subclass does not support adding other SongComponents .

In your specific case, the abstract version would never be called. But you can create another subclass that doesn't override add() that would fall back on the abstract version. You could also have a subclass that calls super.add() within the overridden method.

Remember the abstract class cannot be instantiated but can be called. For an almost exact duplicate question please consult link

I hope this helps.

Any child class of SongComponent , which does not override add,remove methods etc. will get UnsupportedOperationException() .

In your example, SongGroup implemented (overrided) these methods of parent class SongComponent and it does not get those exceptions. If you implement one more class say SongGroup2 which extends SongComponent does not override these methods, will get UnsupportedOperationException()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM