A little thought about Java inheritance

Source: Internet
Author: User

A little thought about inheritance

In Java, inheritance is a way to implement reuse, although many times it is not recommended to use inheritance, but it is undeniable that sometimes it makes inheritance easier to understand a design

I have encountered such a situation, the general operation of object class A instance, but will occasionally operate some instances of Class B, b large part of the attribute A is included, this time using inheritance, should be no problem (at least I now understand that there is no problem, you teach), now put some instances of a and B into an array a[] As in, now to turn the AS, and the B do some operations, this time, you can use instanceof to determine whether B, and then into a B operation, do not know why, the mind is always a bit inconsistent instanceof so I thought of a method, in a and B to add a function IsB () so that you can avoid using instanceof.

In fact, there are some ways to make the operations of B a member function of B, also add a member function with the same name in a, so you can avoid using instanceof, but I think this member function is a unique function of B M1, A.M1 does not have any meaning, feel or add a IsB () More appropriate.

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.