Learning [reproduced] Java abstract classes and interfaces

http://android.blog.51cto.com/268543/385282/
Abstract class abstract class
     It contains abstract methods of the class, called an abstract class . The abstract concept is abstract common attribute: member variables and methods. So abstract class can have a variety of privileges such as private member variables and member methods of non-abstract. Of course, an abstract method is a must .
     An abstract class is a single inheritance, it can not be instantiated . The derived class must implement abstract methods, abstract methods in an abstract class because there is no realization of behavior, can only be access to the public. Rather than abstract method can give the default behavior, you can access a variety, but need to consider whether non-abstract class inherited methods need to be accessed.
Interface interface
     Interface for multiple inheritance, can not be instantiated . Can contain only static final member variables are , however, generally do not define the member variables in the interface. The members of the method in the interface can only abstract methods, access rights can only be public .
 
-----------------
So, whether abstract classes or interfaces are required to implement abstract methods in a subclass, and one less implement these methods in a subclass. And non-abstract methods which abstract class, the subclass may not be implemented inside the override behavior. Refer to the following examples:
  1. public class Child extends Children implements Lover{ 
  2.     public Child(String name) { 
  3.         super(name); 
  4.     } 
  5.  
  6.     public void printName() { 
  7.         System.out.println(super.getName()); 
  8.     } 
  9.  
  10.     public void love(String name) { 
  11.         System.out.println(name + ", I love you!"); 
  12.     } 
  13.  
  14.     public static void main(String[] args) { 
  15.         Child boy = new Child("Charley"); 
  16.         System.out.println(boy.getName()); 
  17.  
  18.         Child girl = new Child("Queenie"); 
  19.         girl.printName(); 
  20.  
  21.         boy.love(girl.getName()); 
  22.         girl.love(boy.getName()); 
  23.     } 
  24.  
  25. abstract class Children { 
  26.     private String name; 
  27.  
  28.     public Children(String name) { 
  29.         this.name = name; 
  30.     } 
  31.  
  32.     //private then Child obj can not access this method 
  33.     //private String getName() { 
  34.     public String getName() { 
  35.         return name; 
  36.     } 
  37.  
  38.     abstract void printName(); 
  39.  
  40.     // haha ​​() in a subclass is not implemented 
  41.     //abstract void haha(); 
  42.  
  43. interface Lover{ 
  44.     void love(String name); 
Application of abstract classes and interfaces
I watched thinking in java content inside, the feeling seems to be an abstract class or interface effects are similar, did not see anything special. So what exactly when to apply abstract class, when the application interface it? Read widespread alarm Door example, to understand the thinking was: an abstract class, "is a" relationship, abstract common essential features, single inheritance; interfaces, "like a" relationship, personalized features, multiple inheritance.
 
They have different door essential characteristics action open (), close (). So abstract classes and interfaces can define these two methods. It has now requires warning alarm function.
1) If these three functions are placed inside an abstract class, so all the doors are equipped with these three functions, no doubt wrong, some doors do not need to alarm ah!
Other Class 2) If these three functions are placed inside the interface, you need to use the alarm function, you need to realize the door open and close function, like this is not right!
 
So, should the door open, close and alarm separation, so that all the doors are open, close the action, inherit the abstract class Door. The need to add the alarm function of the door, then re-inherited interfaces Alarm.
  1. abstract class Door { 
  2.   abstract void open(); 
  3.   abstract void close(); 
  4.  
  5. interface Alarm { 
  6.   void alarm(); 
  7.  
  8. class AlarmDoor extends Door implements Alarm { 
  9.   void open() { … } 
  10.   void close() { … } 
  11.   void alarm() { … } 
 
It can be seen as an abstract class is a single inheritance, interfaces for multiple inheritance, so the need for such arrangements. While at the same time we see that the abstract class is a class of essential characteristics, common; the interface is personalized, you want more personalized classes, inherit other interfaces corresponding personality traits.

Reproduced in: https: //www.cnblogs.com/ericsun/p/3337251.html

Guess you like

Origin blog.csdn.net/weixin_33726313/article/details/93154978