Abstract classClass that contains abstract methods, called abstract classes. The abstract concept is to abstract the common attributes: member variables and methods. SoAbstract classes can have multiple permissions such as private and non-abstract member methods. Of course, abstract methods must be available..Abstract classes are used for single inheritance and cannot be instantiated.. Inheritance classes must implement abstract methods, because abstract methods do not implement behavior in abstract classes, and the access permission can only be public. Instead of an abstract method, you can assign the default behavior of the method with multiple access permissions. However, you need to consider whether non-abstract methods need to be accessed by inheritance classes. InterfaceInterface, used for multi-inheritance, cannot be instantiated.Only static final member variables can be included.But the member variables are not defined in the interface. WhileMember methods can only be abstract methods in the interface, and the access permission can only be public. ----------------- Therefore, abstract methods, whether abstract classes or interfaces, must be implemented in subclasses, and there must be no less to implement these methods in subclasses. The non-abstract methods in the abstract class can be implemented without rewriting the behavior in the subclass. See the following example:
- Public class Child extends Children implements Lover {
- Public Child (String name ){
- Super (name );
- }
-
- Public void printName (){
- System. out. println (super. getName ());
- }
-
- Public void love (String name ){
- System. out. println (name + ", I love you! ");
- }
-
- Public static void main (String [] args ){
- Child boy = new Child ("Charley ");
- System. out. println (boy. getName ());
-
- Child girl = new Child ("Queenie ");
- Girl. printName ();
-
- Boy. love (girl. getName ());
- Girl. love (boy. getName ());
- }
- }
-
- Abstract class Children {
- Private String name;
-
- Public Children (String name ){
- This. name = name;
- }
-
- // Private then Child obj can not access this method
- // Private String getName (){
- Public String getName (){
- Return name;
- }
-
- Abstract void printName ();
-
- // Haha () is not implemented in the subclass
- // Abstract void haha ();
- }
-
- Interface Lover {
- Void love (String name );
- }
I have read thinking in java for abstract classes and interfaces. It seems that the effects of using abstract classes or interfaces are almost the same. So when is the application abstract class and the application interface? After reading the widely-spread alarm door examples, I can understand the following:
Abstract class, "is a" link, abstracts Common Essential features, a single inheritance; interface, "like a" link, personalized features, multiple inheritance.Different doors have essentially characteristic actions: open (), close (). Abstract classes and interfaces can both define these two methods. It is now required to have the alarm function. 1) if these three functions are included in the abstract class, all the doors will have these three functions. No doubt, some do not need the alarm function! 2) if these three functions are put in the interface and other classes of the alarm function need to be used, the open and close functions of the door need to be implemented. This is not the case! Therefore, the open, close and alarm doors should be separated so that all doors have the open and close actions, inheriting the abstract class Door. To add the Alarm function, you must inherit the Alarm interface.
- abstract class Door {
- abstract void open();
- abstract void close();
- }
-
- interface Alarm {
- void alarm();
- }
-
- class AlarmDoor extends Door implements Alarm {
- void open() { … }
- void close() { … }
- void alarm() { … }
- }
It can be seen that the abstract class is used for a single inheritance, and the interface is used for multiple inheritance, so this arrangement is required. At the same time, we can see that abstract classes are the essential features of classes. interfaces are personalized. If you want to make classes more personalized, You can inherit other interfaces with corresponding personalized features.