Basic Java Learning--a simple summary of inheritance

Source: Internet
Author: User
Tags access properties

    • Why introduce inheritance?

If I want to make a media library, inside can put CD, can put DVD. If you make CDs and DVDs into two non-connected classes, then in the management of this media library, you have to do a separate function to add a CD, do a separate function to add DVDs, if you want to add other media classes in this library, but also create another add function. We say that this code is not extensible . In addition, CDs and DVDs have a lot of similarities, their member variables have titles, play length, comments, and so on, we call this program there is a lot of code duplication , code duplication is the performance of bad code. So we can create one of their parent classes, add their same member variables to the parent class, and they can all inherit these variables, and when added to the Media Library just need to create a function that adds their parent class, and they can be added.

    • What does a subclass inherit from a parent class?

Inheritance gets everything from the parent class, including member variables and member functions. But it is not necessarily possible to access everything in the parent class, so look at the access Properties of the member variables and member functions inherited by the parent class. If a member variable in the parent class is private, then the subclass is not directly accessible. However, private only can be accessed by the parent class, so we can access these member variables indirectly by calling the public function of the parent class in the subclass.

In addition, subclasses can have their own unique member variables and functions in addition to those inherited from the parent class, written in their own class.

    • Accessing properties
Accessing properties Meaning
Public Open to all
Private Only yourself can access
Protected Only yourself, subclasses, and other classes within the package can access
Default Only yourself and other classes within the package can access

    • Super ();
      • Member variables constructed in the constructor of the parent class, the parameter table of the constructor of the subclass to be written, and the member variables that were constructed in the parent class are taken from the parent class with super (). For instance code, see the Polymorphic blog post.
      • Super (); The arguments inside will determine which constructor in the parent class is called, Super (), and the parameters in the constructor are the same as the arguments that are called.
      • In the default mode (such as no super () in the Fruit class constructor), the constructor with no arguments in the parent class is called by default, and Eclipse will error if there is no such constructor in the parent class.

    • Overwrite (override)

When a function with the same name is in the subclass and the parent class, the function in the subclass overrides (override) the function of the same name inherited from the parent class, and the function from the parent class is hidden and does not work.  But if you want to call that same name function in the parent class in the subclass, write super. function name ();

Basic Java Learning--a simple summary of inheritance

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.