Java Abstract classes and interfaces

Source: Internet
Author: User

Original: http://www.xl7788.com/zt/computerprogram/JavaInterface.html

 

The quality of a software design depends to a large extent on its overall architecture. The overall architecture is actually your abstract framework for the entire macro business, when the structure of the high-level abstraction layer representing the business logic is reasonable, you only need to consider some algorithms and specific business implementations. When you need to develop another similar project, your previous abstraction layer may be able to be used again. In the face of Object design, the focus of reuse should be the reuse of the abstraction layer, instead of reusing a specific code block, do you suddenly feel that your understanding of reuse has increased?

When it comes to abstraction, I can't help but mention the Java interfaces and Java Abstract classes that once gave me a headache. This is also the focus of this article.

Since the focus of object-oriented design lies in abstraction, Java interfaces and Java Abstract classes will inevitably exist.

Java interfaces and Java Abstract classes represent abstract types, which are the specific manifestations of the abstraction layer we need to propose. To improve the Reuse Rate of programs, increase the maintainability and scalability of programs, the OOP object-oriented programming must be interface-oriented and abstract-oriented, correctly Use interfaces and abstract classes as the top layers of your structural hierarchy.

There are too many similarities between Java interfaces and Java Abstract classes, and there are too many special points. What exactly is the best place for Java interfaces and Java Abstract classes? By comparing them, you can find out.

  1. The biggest difference between a Java interface and a Java Abstract class is that a Java Abstract class can provide partial implementation of some methods, but a Java interface cannot. This is probably the only advantage of a Java Abstract class, however, this advantage is very useful. For example, if a new method is added to an abstract class, all its subclasses will get this new method at once, and the Java interface cannot do this, if you add a new method to a Java interface, all classes that implement this interface cannot be compiled successfully, because you must make every class implement this method again, this is obviously a disadvantage of the Java interface.
  2. The implementation of an abstract class can only be given by the subclass of this abstract class. That is to say, this implementation is in the hierarchy defined by the abstract class. Due to the single inheritance of the Java language, therefore, the efficiency of an abstract class as a type definition tool is greatly reduced. At this point, the advantages of the Java interface come out. Any class that implements the method specified by a Java interface can have the type of this interface, A class can implement any number of Java interfaces, so this class has multiple types.
  3. From the 2nd point, it is not difficult to see that the Java interface is an ideal tool for defining the hybrid type. The mixed class indicates that a class has not only a primary type, but also other secondary behaviors.
  4. Combined with the advantages of abstract classes and Java interfaces in, the classic design pattern came out: the Declaration type work is still undertaken by the Java interface, but a Java Abstract class is also provided, this interface is implemented, while other specific classes belonging to this abstract type can choose to implement this Java interface, or inherit this abstract class, that is, in the hierarchy, the Java interface is at the top, followed by the abstract class. Ha, the biggest advantages of the two can be brought to the extreme. This mode is the "Default adaptation mode ". This mode is used in Java APIs and all follow certain naming rules: Abstract + Interface Name.

Java interfaces and Java Abstract classes exist for implementation and Inheritance of specific classes. If you want to write a specific class to inherit another specific class, then there is a big problem with your design. The Java Abstract class exists for inheritance, and its abstract method is to force the subclass to be implemented.

Use Java interfaces and abstract Java classes to declare the types of variables, declare parameters as types, return methods, and convert data types. Instead of using a specific Java class to declare the type of the variable, the parameter is the type declaration, the return type description of the method, and the conversion of the data type.

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.