When creating an abstract class whose processing is not decided at this time Use ** abstract **
sample
public abstract class Classicmusic {
//Abstract class about classical music
public abstract String getCountry();
//Abstract method to get the country of origin
//return Country of origin of composer
public abstract String[] getmusicList();
//Abstract method to get song list
//return List of representative songs
A class that implements even one abstract method ** must be an abstract class with abstract **. And the abstract class cannot be instantiated with new. Note) It is possible to implement non-abstract methods in abstract classes as well.
The abstract method is implemented by the ** child class to determine the content of the process. ** ** Use extends ** when inheriting ** abstract.
sample
public class Bach extends Classicmusic {
public String getCountry() {
return "Germany";
}
public String[] getmusicList() {
String[] mList = {"Goldberg Variations","Italian Concerto","English Suite"};
return mpList;
}
A class that inherits an abstract method must implement the abstract method. The content of the process is ** determined by the inherited class **. Determining the contents of a method that was undecided until then is called ** implementation **. If the inherited abstract method is not implemented, a compile error will occur.
This allows you to delegate the implementation to the inherited child class. In this way, by making the process that the content to be implemented is not decided but must be implemented as an abstract method, ** Can force implementation **.
Classes with a particularly high degree of abstraction can be treated as ** interfaces **.
As a condition to treat as an interface **-All methods are abstract methods ** ** · Has no fields ** Need to be
sample
public abstract class Classicmusic {
public abstract String getCountry();
public abstract String[] getmusicList();
}
//The interface can be as follows.
public interface Classicmusic {
String getCountry();
String[] getmusicList();
}
//If you want to implement the interface, use implements instead of extends.
public class Bach implements Classicmusic
Recommended Posts