Java does not support multi-Inheritance

Source: Internet
Author: User

Q: Does Java support multi-inheritance?

A: Unfortunately, indeed. Almost any textbook writes this way: Java follows a single inheritance structure.

 

Q: Why must we inherit more?

A: for example, the sofa bed is both a sofa and a bed.

 

Q: What does inheritance mean?

A: inheritance means the "is-a" relationship. However, this statement is not accurate. In many ancient C ++ books, inheritance represents two relationships: Is-A and like-.

 

Q: What does like-a mean?

A: Don't forget. For the early programmers who just migrated from C to C ++, that is, programmers who just switched from process-oriented to object-oriented programming, the most prominent part of inheritance is code reuse. I have reason to believe that a C ++ programmer inherits a class, not because "A is a B", but because "A looks very similar to B, and there are only slight differences between the two ", inheritance and rewriting can save a lot of code.

In fact, even in Java programming, I often do this.

 

Q: Is it necessary to like-?

A: When object-oriented theories and languages have matured to this day. We have enough reason to sweep the inheritance relationship of like-a into the garbage. Through careful and rigorous design and analysis, we can construct an inheritance structure that strictly satisfies the is-a relationship. For example, we strip the public part of A and B that looks very similar, and use it as an abstract class C. Then a and B inherit from C respectively. How good is this!

Unfortunately, this is just a theory. The reality is: even in the standard API of j2se, the most authoritative Java program still has this inheritance relationship like-. For example, java. SQL. Data class.

 

Q: What is wrong with like-?

A: The evil of like-A is that it destroys the substitution of inheritance.

Inheritance based on is-a is irreplaceable, that is, any place where the parent class can appear can be replaced by subclass. Because the sub-class "is a" parent class, everything that the parent class can do can be done by the sub-class.

Like-a breaks this convention. Because the two ends of the like-a relationship are just "looks like" and do not have a logical "yes" relationship. Therefore, it is not guaranteed that child classes can do anything that the parent class can do.

After all, the inheritance of like-a is only for the convenience of code reuse. It does not guarantee the true logical relationship between the two!

 

Q: What does interface inheritance mean?

A: Obviously, the implementation of the interface must be "inheritance" of is-". If I can use implements as an inheritance.

Reason: like-a inherits only code reuse. In the interface, only the method declaration has no method body, and there is no possibility of code reuse.

 

Q: Is-A inheritance for interfaces different from is-a inheritance for parent classes?

A: There are differences.

When inheriting (implementing) an interface, our motivation is "simple": Inheriting the interface.

When inheriting a parent class, we not only inherit all interfaces of the parent class, but also inherit the implementation of the parent class. That is to say, it includes the purpose of code reuse, that is, it potentially contains the inheritance of like-.

 

Q: What is the difference between an inherited interface and an inherited implementation? What is the relationship?

A: First, implement the interface. Therefore, if the implementation is inherited, the interface must be inherited first.

This also explains why Java only inherits interfaces and implementations (extends), but not implementations.

At the same time, the goal of is-A is to inherit interfaces, but may be accompanied by the implementation of inherited interfaces.

The purpose of like-A is to inherit the implementation (code reuse). Although it inherits the interface, it is just a last resort. The inherited interfaces may also be modified and damaged without any protection. This is why like-a destroys the alternative of inheritance.

 

Q: In this case, the full inheritance of interfaces ensures that the inheritance can be used as a replacement?

A: It is completely correct.

 

Q: Is there a day when we can clearly distinguish the inheritance of interfaces from the inheritance of implementations?

A: I expect a new language to provide this function one day. If this language mechanism is added to Java, I think this may be the case. (Note: it is a language mechanism rather than an API !)

Public Class A implements (interface of B ){...}

That is to say, we can directly extract the interface of a class from this class. In this way, Java's multi-inheritance solution based on interfaces and internal classes will be more perfect.

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.