Why is there an abstract interface decorated class in Java?

Source: Internet
Author: User

If someone asks you why you have an abstract interface modified class, the answer must be that the way he sees it must be the result of the decompile. There is no difference between abstract interface and interface-modified classes in practice.

The following is an introduction to the concept of abstract interfaces.

In the process of programming, the reader is likely to encounter such a dilemma: the design of an interface, but the implementation of the sub-class interface does not need to implement all the methods in the interface, that is, there are too many methods in the interface, for some subclasses is redundant, we have to waste to write an empty implementation.

The "abstract interface" mentioned today is used to solve this problem.

To not mislead the reader, first explain what is an "abstract interface."

The so-called "abstract interface", that is, provides an interface while providing an abstract class, with an abstract class implementation of the interface (in fact, this is the default adaptation mode).

Here are some examples of the benefits that the reader has to make.

The code does not write, in order to prevent readers do not understand, first on a class diagram:

Specific code:

Itestinterface.java

     /* Assume there is a top -level interface * /interface itestinterface{void method1 ();  int method2 ();  Boolean method3 ();  8}          

Testabstract.java

1/*2Abstract class abstraction implements the Itestinterface top-level interface3*/45PublicAbstractClass TestabstractImplementsitestinterface{6//Find the necessary method in the interface, that is, the subclass must implement the method, defined as an abstract method, by the subclass implementation7Publicabstract void Method1 ( );  8 public abstract int Method2 ();  9 10 Span style= "color: #008000;" >//11 public boolean method3 {12 return true; 13 }14}     

  

Testclass1.java

1/*2The ordinary class TestClass1 inherits the Testabstract abstract class3*/45PublicClass TestClass1Extendstestabstract{67//TestClass1 must implement an abstract Method1 method, which is first defined in the interface8Publicvoid Method1 () { 9 10 }11 //testclass1 must implement an abstract Method2 method, which is the first defined in the interface 12 public Span style= "color: #0000ff;" >int Method2 () {13  return 1; }15 16 //17}             

Testclass2.java

1/*2The ordinary class TestClass2 inherits the Testabstract abstract class3*/45PublicClass TestClass2Extendstestabstract{67//TestClass2 must implement an abstract Method1 method, which is first defined in the interface8PublicvoidMethod1 () {910}11//TestClass2 must implement an abstract Method2 method, which is first defined in the interface12Publicint Method2 () {13 return 2; }15 16 //method3 method is important for TestClass2 and must be overridden. 17 public boolean Method3 () {18 return  False; }20 21}   

Code explaining:

As can be seen from the above example, the interface of the highest layer is implemented by an abstract class, in which we define the key method1 and Method2 methods as abstract methods, forcing subclasses to implement, and the "unique" method3 method to do a default implementation in the abstract class.

Wait until TestClass1, TestClass2 inherit Testabstract abstract class, the advantage is manifested, TESTCLASS1, TestClass2 must implement METHOD1, METHOD2, but if not used method3, can be ignored directly.

Through the combination of interfaces and abstract classes, avoids a large number of "meaningless" implementations in the subclasses that implement the interface, this "meaningless" implementation is buffered into an abstract class, which perfectly demonstrates the code reuse (which can be understood as the buffer between the interface and the implementation class).

It should be pointed out that we can either inherit the abstract class or choose to implement the interface, not necessarily to inherit the abstract class, see the situation, here are two options, two opportunities.

Write to this, perhaps readers think the article is over, in fact, there is no ...

The benefit of doing this is not only this, but savor it if we add a method to the interface ...

Specific code:

warm tip: do not be frightened by the code, in fact, the code and the top of the same, but added a method just.

Itestinterface.java

1/*2 Suppose there is a top-level interface  3 */ 4 public interface 5 void 6 int Method2 ();  7 boolean method3 ();  8 // 9  String method4 (); 10}             

Testabstract.java

1/*2Abstract class abstraction implements the Itestinterface top-level interface3*/45PublicAbstractClass TestabstractImplementsitestinterface{6//Find the necessary method in the interface, that is, the subclass must implement the method, defined as an abstract method, by the subclass implementation7PublicAbstractvoidMethod1 ();8PublicAbstractIntMethod2 ();910//Some unique methods can be implemented by default in abstract classes11 public boolean Method3 () {12 return  True; }14 15 // A default implementation is provided in the abstract class to avoid "alerting" all subclasses 16 public String method4 () {17 return "" ; }19}        

Testclass1.java

1/*2The ordinary class TestClass1 inherits the Testabstract abstract class3*/45PublicClass TestClass1Extendstestabstract{67//TestClass1 must implement an abstract Method1 method, which is first defined in the interface8PublicvoidMethod1 () {910}11//TestClass1 must implement an abstract Method2 method, which is first defined in the interface12PublicIntMethod2 () {13return 1;}15 16 //17 18 //19 public String method4 () {20 return "Class1" ; 21 }22 23}                 

Testclass2.java

1/*2The ordinary class TestClass2 inherits the Testabstract abstract class3*/45PublicClass TestClass2Extendstestabstract{67//TestClass2 must implement an abstract Method1 method, which is first defined in the interface8PublicvoidMethod1 () {910}11//TestClass2 must implement an abstract Method2 method, which is first defined in the interface12PublicIntMethod2 () {13Return 214 }15 16 //method3 method is critical for TestClass2 and must be overridden. 17 public boolean Method3 () {18 return  False; }20 21 //22}  

Code explaining:

This code shows that if the project has been formed, but the requirements have changed, we have to add a new method to the interface, if the sub-class directly implement the interface, then these subclasses are modified to implement the interface new method.

But in this case, the TESTCLASS1, TestClass2 subclasses do not directly implement the interface, but by inheriting the abstract class to implement the interface indirectly, so that the benefits are reflected!

The new method to the interface can be buffered in the abstract class that implements the interface, providing a default implementation, so that you do not have to enforce all subclasses ( classes that indirectly implement an interface by inheriting an abstract class) are modified so that the image can be interpreted as "without disturbing the child class". Instead, you need to use the subclass of this method to rewrite it directly.

Side-dishes feeling:

Man's wisdom is great! Arrays and linked lists combine to produce an efficient hash table, and interfaces and abstract classes combine to produce an elegant default adaptation pattern. Everybody work hard!!!

Written in the following words:

There is no perfect thing in the world, the design pattern is so, too much discussion pros and cons no meaning, the right is the best, what is appropriate? This is where wisdom is embodied.

Transferred from: http://www.cnblogs.com/iyangyuan/archive/2013/03/11/2954808.html

Yang Yuan

Why is there an abstract interface decorated class in Java?

Related Article

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.