My understanding of object-oriented design-conversion of Java interfaces and Java Abstract classes

Source: Internet
Author: User

In the absence of a good study of object-oriented Design Before the design pattern, I had a vague understanding of Java interfaces and Java Abstract classes.

When I first learned the Java language, it was hard to understand why the concept of interfaces was required. Although multi-inheritance can be implemented, there is only a method name and no method body, how can I implement it? I can't get anything from it. Except for some method names, can I simply add these methods to a specific class?

Why must we have the abstract class concept? Why can't I write this parent class as a specific class, and can't the Child class inherit from it? Why do you need to get an abstract class and abstract methods without method bodies? It's confusing to make them look like interfaces and classes.

When I start Learning The Java design pattern, after truly entering the door of object-oriented design, I found that my understanding of object-oriented design was so one-sided and so superficial, there is no real understanding of the essence of Object-oriented thinking at all. To a certain extent, it is still affected by process-oriented thinking that a class is created, even if it is object-oriented, it is actually driven by the process.

Let me talk about my current understanding of Object-oriented thinking, which is not necessarily correct and comprehensive, but I think it is a little better than before.

Object-oriented thinking, I think the most important thing is abstraction.

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 it represents the high-level abstraction layer structure of business logic
When it is reasonable, you need to consider the specific implementation of the underlying layer.AlgorithmAnd some specific business implementation. When you need to develop another similar project, your previous abstraction layer may be able to use it again.
In the face of Object design, the focus of reuse should be the reuse of the abstract layer, rather than a specificCodeBlock reuse: Does it suddenly feel that I have a higher level of understanding about reuse? Pai_^

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. Oop object-oriented programming, if you want to improveProgramTo increase the program reuse rate.
For maintainability and scalability, it must be interface-oriented programming, abstract-oriented programming, correct use of interfaces, abstract classes, which are too useful abstract types as the top layer 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 Java interfaces and Java Abstract classes is that Java Abstract classes can provide partial implementation of some methods, but Java interfaces cannot. This is probably the only advantage of Java Abstract classes, however, this advantage is very useful.
If a new concrete 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
New method, all the classes that implement this interface cannot be compiled successfully, because you must make every class implement this method again, which 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. Combining the advantages of abstract classes and Java interfaces in and, the classic design pattern came out: declarative Work Still
However, it is assumed by the Java interface, but a Java Abstract class is provided at the same time, and this interface is implemented. Other specific classes belonging to this abstract type can choose to implement this Java interface, or you can choose
Inherit this abstract class, that is to say, 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 "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.

I think, if you don't even have an interface or an abstract class in your code, maybe I can say that you didn't use any design pattern at all. Any design pattern is inseparable from the abstraction, abstract and Java interfaces and abstract Java classes are inseparable.

Understanding abstraction, understanding Java interfaces, and abstract Java classes should actually start to analyze problems with object-oriented ideas and solve the problems.

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.