"Abstract interface" in Java"

Source: Internet
Author: User

InProgramDuring the design process, readers may encounter the following dilemma: an interface is designed, but the subclass of the interface does not need to implement all the methods in the interface, that is, there are too many methods in the interface, some subclasses are redundant and we have to waste writing an empty implementation.

The abstract interface mentioned by Xiao Cai today is used to solve this problem.

To avoid misleading readers, let's first explain what an abstract interface is ".

The so-called "abstract interface" provides an abstract class while providing the interface, and implements the interface using the abstract class (in fact, this is the default adaptation mode ).

The following is an example of a dish to help readers understand the benefits of doing so.

CodeTo prevent readers from reading it, first create a class diagram:

Code:

Itestinterface. Java

1 /*2 Assume that there is a top-level interface3 */4 Public InterfaceItestinterface {5VoidMethod1 ();6IntMethod2 ();7BooleanMethod3 ();8}

 

 

Testabstract. Java

1   /*  2   Abstract class implements the top-level interface of itestinterface  3   */  4   5   Public   Abstract   Class Testabstract Implements  Itestinterface {  6       //  Find the necessary methods in the interface, that is, the methods that must be implemented by sub-classes, define them as abstract methods, and submit them to sub-classes for implementation. 7       Public   Abstract   Void  Method1 ();  8       Public   Abstract   Int  Method2 ();  9       10       //  Some unique methods can be implemented by default in abstract classes.  11       Public   Boolean Method3 (){  12           Return   True  ;  13   }  14 }

 

Testclass1.java

 1   /*  2   The common class testclass1 inherits the testabstract abstract class.  3   */  4  5   Public   Class Testclass1 Extends  Testabstract {  6       7       //  Testclass1 must implement the abstract Method1 method, which was first defined in the interface  8       Public   Void  Method1 (){  9           10   } 11       //  Testclass1 must implement the abstract method2 method, which was first defined in the interface  12       Public   Int  Method2 (){  13           Return 1 ;  14   }  15       16       //  The method3 method in the interface is irrelevant to testclass1, so it is not overwritten. 17 }

 

 

Testclass2.java

 1   /*  2   The common class testclass2 inherits the testabstract abstract class.  3   */  4   5   Public   Class Testclass2 Extends  Testabstract {  6      7       //  Testclass2 must implement the abstract Method1 method, which is first defined in the interface  8       Public   Void  Method1 (){  9       10   }  11       //  Testclass2 must implement the abstract method2 method, which was first defined in the interface  12       Public   Int Method2 (){  13           Return 2 ;  14   }  15       16       //  The method3 method is critical to testclass2 and must be rewritten.  17       Public   Boolean  Method3 (){  18           Return   False ;  19   }  20       21 }

 

 

Code:

From the above example, we can see that the interface at the highest level is implemented by an abstract class. In the abstract class, we define the key Method1 and method2 methods as abstract methods and force the subclass to implement them, the "unique" method3 method is implemented by default in the abstract class.

When testclass1 and testclass2 inherit the testabstract class, the advantages are shown. testclass1 and testclass2 must implement Method1 and method2, but if method3 is not used, you can ignore it directly.

Through the combination of interfaces and abstract classes, a large number of "meaningless" implementations are avoided in the subclass implementing interfaces. This "meaningless" implementation is buffered into the abstract class, code reuse is perfectly demonstrated (the abstract class can be understood as the buffer between interfaces and implementation classes ).

It should be noted that we can choose to inherit abstract classes or implement interfaces, not to say that we must inherit abstract classes. Depending on the situation, there are two options and two opportunities.

Readers may think thatArticleIt's over. Actually, no...

The benefit of doing so is not just that, but to savor it, if we add a method to the interface...

 

Code:

Tip:Don't be scared by the Code. In fact, these codes are similar to the above Code, but they just add a method.

 

Itestinterface. Java

 1   /*  2   Assume that there is a top-level interface  3   */  4   Public  Interface  Itestinterface {  5       Void  Method1 ();  6       Int  Method2 ();  7       Boolean  Method3 ();  8       //  Method added in the interface  9   String method4 ();  10 }

 

 

Testabstract. Java

 1   /*  2   Abstract class implements the top-level interface of itestinterface  3   */  4   5   Public   Abstract   Class Testabstract Implements  Itestinterface {  6      //  Find the necessary methods in the interface, that is, the methods that must be implemented by sub-classes, define them as abstract methods, and submit them to sub-classes for implementation.  7       Public   Abstract   Void  Method1 ();  8       Public   Abstract   Int  Method2 ();  9       10       //  Some unique methods can be implemented by default in abstract classes.  11      Public   Boolean  Method3 (){  12           Return   True  ;  13   }  14       15       //  The abstract class provides a default implementation to avoid "disturbing" All subclasses.  16       Public  String method4 (){  17          Return "" ;  18   }  19 }

 

 

Testclass1.java

 1   /*  2   The common class testclass1 inherits the testabstract abstract class.  3   */  4   5   Public   Class Testclass1 Extends  Testabstract {  6       7       //  Testclass1 must implement the abstract Method1 method, which was first defined in the interface  8       Public   Void  Method1 (){  9           10   }  11       //  Testclass1 must implement the abstract method2 method, which was first defined in the interface 12       Public   Int  Method2 (){  13           Return 1 ;  14   }  15       16       //  The method3 method in the interface is irrelevant to testclass1, so it is not overwritten.  17       18       // The new method is critical to testclass1, so it must be rewritten.  19       Public  String method4 (){  20           Return "Class1" ;  21   }  22   23 }

 

 

Testclass2.java

 1   /*  2  The common class testclass2 inherits the testabstract abstract class.  3   */  4   5   Public   Class Testclass2 Extends  Testabstract {  6       7       //  Testclass2 must implement the abstract Method1 method, which is first defined in the interface  8       Public   Void Method1 (){  9       10   }  11       //  Testclass2 must implement the abstract method2 method, which was first defined in the interface  12       Public   Int  Method2 (){  13           Return 2 ;  14   }  15      16       //  The method3 method is critical to testclass2 and must be rewritten.  17       Public   Boolean  Method3 (){  18           Return   False  ;  19   }  20       21       // The new method does not matter for testclass2. You do not need to know the existence of the new method4.  22 }

 

 

Code:

This Code demonstrates that if the project has been built but the demand has changed, we have to add a new method to the interface. If the subclass directly implements the interface, these subclasses must be modified, to implement the new method of the interface.

However, the testclass1 and testclass2 subclasses in this example do not directly implement the interface, but inherit the abstract class to indirectly implement the interface, so the benefits will be reflected!

The method added to the interface can be buffered in the abstract class of the implemented interface to provide a default implementation, so that you do not have to force all subclasses (Class that indirectly implements interfaces by inheriting abstract classes)And can be interpreted as "no disturbing subclass ". You can directly rewrite the subclass using this method.

 

Side dishes:

 

The wisdom of mankind is really great! The combination of arrays and linked lists produces efficient hash tables. The combination of interfaces and abstract classes produces an elegant default adaptation mode. Everyone should work hard !!!

 

Below:

There is no perfect thing in the world, so is the design pattern. Too much discussion about the advantages and disadvantages is meaningless. The right thing is the best. What is the right thing? This is the embodiment of wisdom.

 

 

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.