Java basic learning -- a simple summary of inheritance, java --

Source: Internet
Author: User

Java basic learning -- a simple summary of inheritance, java --


Code reference:Java basic learning note-Polymorphism

 

  • Why do we need to introduce inheritance?

You can also create a media library with CD and DVD. If CD and DVD are made into two unrelated classes, you need to create a function to add a CD and a function to add a DVD when managing the media library, if you want to add other media classes to the library, create another function. Let's talk about this code.Not scalable. In addition, there are many similarities between CD and DVD. Their member variables contain titles, playback durations, comments, and so on. We call such programs a large numberCode Replication, Code replication is a poor performance of the Code. Therefore, we can create a parent class and add the same member variables to the parent class. Both of them can inherit these variables, you only need to create a function to add their parent class when adding them to the media library, and they can also be added.

  • What does a subclass inherit from its parent class?

Inherited to get everything of the parent class, including member variables and member functions. But it does not necessarily have to be able to access everything in the parent class. In this way, we can see the member variables inherited by the parent class and the member functionsAccess attributes. If the member variables in the parent class are private, the subclass cannot be accessed directly. However, private can only be accessed by the parent class, so we can indirectly access these member variables in the subclass by calling the public function of the parent class.

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

  • Access attributes
Access attributes Description
Public Open to all
Private Accessible only by yourself
Protected Only you, subclass, and other classes in the package can access
Default Only you and other classes in the package can access

 

  • Super ();
    • Member variables constructed in the constructor of the parent class, which must be specified in the parameter table of the constructor of the subclass, and super () is used () take these member variables constructed in the parent class from the parent class. For more information about the instance code, seePolymorphismThat blog post.
    • The parameters in super (); Determine which constructor is called in the parent class, and the parameters in super () are the same as those in the called constructor.
    • In the default mode (if the subclass constructor does not have super ();), the constructor without parameters in the parent class is called by default. If the parent class does not have such constructor, eclipse reports an error.
  • Override)

When the subclass and the parent class have functions with the same name, the function in the subclass will goOverride)The same name function inherited from the parent class, so the function from the parent class is hidden and does not work. However, if you want to call the function of the same name in the parent class in the subclass, you should writeSuper. Function Name ();.

 

Related Article

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.