It's so hard for Java to inherit.

Source: Internet
Author: User
Tags addall

Imagine a problem:

If we need to give a super-class method to achieve a more powerful function, that is, the enhanced version of the super-class, what will generally do?

Inherited?

Too Young Too simple!

Take a look at the following example:

When we need a class, we need all the methods of the HashSet class, but we need to know how many elements have been added to it at any time, how to implement it?

The general use of inheritance, overwriting the Add () and AddAll () methods, would seem reasonable:

1  Public classInstrumentedhashset<e>extendsHashset<e>{2     Private intMCount;3     4      Public intGetmcount () {5         returnMCount;6     }7 @Override8      Public BooleanAdd (e e) {9MCount + +;Ten         return Super. Add (e); One     } A @Override -      Public BooleanAddAll (collection<?extendsE>e) { -MCount + =e.size (); the         return Super. AddAll (e); -     } -}

Lines 9th and 14th have increased the mcount, so is it really as we imagined?

1      Public Static voidMain (string[] args) {2Instrumentedhashset<string> Mhashset =NewInstrumentedhashset<string>();3         4Arraylist<string> marraylist =NewArraylist<string>();5Marraylist.add ("1");6Marraylist.add ("2");7Marraylist.add ("3");8     9 Mhashset.addall (marraylist);TenSystem.out.println ("inserted in total" +Mhashset.getmcount ()); One          A}

Final output: A total of 9 were inserted

Why?

This is because, in the implementation of HashSet, the AddAll () method is called the Add () method, although the pit daddy, but there is no need to explain in the documentation. In this class, as long as we remove the overlay of add (), we can run the program well.

So, the question comes, in the future, when encountering this demand, do not use inheritance?

Maybe you think this is an example, just be careful, and when you want to use inheritance, answer the following questions:

1. What if such a parent class changes in a later version?

2. What if the parent joins a new method and does not implement it?

Even, you might think I'm using inheritance, but it's safe not to overwrite the original method? Consider the following questions:

1. If a superclass adds a method, and the function signature of the method you provide to the subclass happens to be the same as it is, but the result is different. Then, the compiler does not pass this method.

2. If the function signature and return type are the same, does that not inherit again?

Fortunately, there is a way to solve the above problem, it is the seemingly humble-combination (compound)!

Thus, the original class becomes a component of the new class, and each example method in the new class can invoke the corresponding method in the sample containing the existing class and return its result, which is called " forwarding ," and the method in the new class is called the forwarding method .

Such a class is very robust, and does not affect new classes even if new methods are added to existing classes.

Inheritance can only be used when a subclass is a subtype of a true superclass, that is, when the "is-a" relationship must exist.

It's so hard for Java to inherit.

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.