Why is it so difficult to use an inheritance program in Java?

Source: Internet
Author: User

Why is it so difficult to use an inheritance program in Java?

Imagine a problem:

If we need to implement a stronger function for a super-class method, that is, the enhanced version of the super-class, what will happen in general?

Inherit?

Too young too simple!

 

Take a look at the following example:

When we need a class that requires all the methods of the HashSet class, but we need to know at any time how many elements have been added to its creation so far?

It is reasonable to use inheritance to overwrite the add () and addAll () methods:

 1 public class InstrumentedHashSet<E> extends HashSet<E>{ 2     private int mCount; 3      4     public int getmCount() { 5         return mCount; 6     } 7     @Override 8     public boolean add(E e) { 9         mCount ++;10         return super.add(e);11     }12     @Override13     public boolean addAll(Collection<? extends E> e) {14         mCount += e.size();15         return super.addAll(e);16     }17 }

Rows 9th and 14th increase mCount. Is it as we thought?

1 public static void main (String [] args) {2 instruments entedhashset <String> mHashSet = new InstrumentedHashSet <String> (); 3 4 ArrayList <String> mArrayList = new ArrayList <String> (); 5 mArrayList. add ("1"); 6 mArrayList. add ("2"); 7 mArrayList. add ("3"); 8 9 mHashSet. addAll (mArrayList); 10 System. out. println ("Total inserted" + mHashSet. getmCount (); 11 12}

 

Final output: a total of 9 inserted

Why?

In fact, in the implementation of HashSet, The addAll () method calls the add () method. Although this is a problem, it is not necessary to explain it in the document. In this class, as long as we remove the overwrite of add (), we can run the program well.

So the question is, do you need to inherit from this kind of requirement in the future?

You may think this is an example. Just pay attention to it. Then, when you want to use inheritance, answer the following questions:

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

2. What if the parent class is added with a new method but is not implemented by itself?

Even more, do you think that I use inheritance, but it is safe to not 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, but the returned results are different. Then, the compiler will not use this method.

2. If both the function signature and the return type are the same, isn't it inherited?

Fortunately, there is a way to solve the above problems, that is, the seemingly inconspicuous combination )!

In this way, the original class becomes a component of the new class. Each sample method in the new class can call the corresponding method in the existing class example and return its results. This is called"ForwardingThe method in the new class is calledForwarding Method.

Such a class is very stable. Even if a new method is added to the existing class, the new class will not be affected.

 

Inheritance can be used only when the subclass is a real subtype of the super class. That is to say, inheritance is applicable only when the "is-a" relationship exists.

 

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.