Why is it prudent to use inheritance in Java?

Source: Internet
Author: User
Tags addall

There are two unavoidable drawbacks to using inheritance in Java:

Breaking the encapsulation, forcing the developer to understand the implementation details of the superclass, subclasses and super-class coupling. A hyper-class update may cause an error. Inheritance breaks the encapsulation

For this, here is a detailed example (from effective Java 16th)


Here is a custom hashset, overriding two methods, and the only difference between it and the superclass is to add a counter to count the number of elements added.

Write a test to test whether this new feature works:


After running, it is found that after adding 3 elements, the counter output value is 6.

the AddAll () method in the superclass finds the cause of the error: it calls the Add () method internally. So in this test, into the subclass of the AddAll () method, the number of the device plus 3, and then call the superclass of the AddAll (), the superclass of AddAll () will call the subclass of Add () three times, then the counter will add three.

The root cause of the problem

By abstracting the situation, you can see that the error is due to the fact that the overriding method of the superclass is self-contained (that is, the overridden method in the superclass calls other methods that can be overridden), which can result in errors if the subclass overrides some of these methods.


a hyper-class update may produce an error

This is a good understanding, mainly in the following possibilities:

Does not conflict with subclasses, but may affect the correctness of the program. For example, a subclass of elements to join a set must meet certain conditions, when the superclass is added to a method without detection can directly insert elements, the correctness of the program is threatened.

When designing classes that can inherit from class design, you should be aware of:

For an overriding method that exists for self-use, the details of the invocation should be accurately described in the document. Exposing protected members as little as possible, or exposing too much implementation detail. The constructor should not call any methods that can be overridden. Explain in detail the 3rd. It is actually similar to the problem discussed in inheritance breaking the encapsulation, assuming the following code:



The code above throws NullPointerException when it runs the test:


Because the constructor of the superclass runs before the constructor of the subclass, where the superclass constructor has a dependency on somemethod () and SomeMethod () is overridden, the constructor of the superclass is called Son.somemethod (), When the subclass is not initialized, a null pointer exception is thrown when running to Date.gettime ().

Therefore, if you have dependencies on a method that can be overridden in a superclass constructor, you may get an error when inheriting.

Conclusion use inheritance cautiously, and compound takes precedence over inheritance.

Overridden methods that override the use of a superclass in an inheritance may cause errors, even if overridden, to introduce an error in the Hyper-class update.

If you use inheritance and compositing, then the precedence of the composite, the above on the inheritance of the shortcomings can be combined to avoid.

If you want to use inheritance, you should carefully design the superclass and provide detailed documentation.

If you have difficulties in the development of Java, or learning to encounter bottlenecks, welcome to join our Java Total Group: 309603235, the group has Java technology Daniel, welcome rookie, the birds into the pit.


Why is it prudent to use inheritance in Java?

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.