Learning Java Abstract classes and interfaces

Source: Internet
Author: User

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:

 
 
  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.  
  26. Abstract class Children {
  27. Private String name;
  28.  
  29. Public Children (String name ){
  30. This. name = name;
  31. }
  32.  
  33. // Private then Child obj can not access this method
  34. // Private String getName (){
  35. Public String getName (){
  36. Return name;
  37. }
  38.  
  39. Abstract void printName ();
  40.  
  41. // Haha () is not implemented in the subclass
  42. // Abstract void haha ();
  43. }
  44.  
  45. Interface Lover {
  46. Void love (String name );
  47. }
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.
 
 
  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 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.