The relationship between the Java subclass and the parent class

Source: Internet
Author: User
Tags access properties

Excerpt from Onge Teacher's MOOC

The most important thing to understand about inheritance is to know what is inherited, or what the subclass gets from the parent class. The answer is: all things, all the members of the parent class, including the variables and methods, become members of the subclass, in addition to the constructor method. Constructor methods are unique to the parent class, because their name is the name of the class, so the constructor of the parent class does not exist in the subclass. In addition, subclasses inherit all members of the parent class.

but get not equal to be able to use casually. Each member has a different access property, and the subclass inherits all the members of the parent class, but the different access properties make the subclasses different when they use these members: some of the members of the parent class directly become the external interface of the subclass, and some are deeply hidden, even though the subclass itself cannot be accessed directly. The following table lists the access properties of the parent class members of the different access properties in the subclass.

Parent class member Access properties

Meaning in the parent class

Meaning in a subclass

public

open to everyone

open to everyone

protected

only other classes within the package, themselves and subclasses can access

only other classes within the package, themselves and subclasses can access

Default

Only other classes within the package can access

You cannot access

Private

Only yourself can access

Cannot access

PublicThe members of the group directly become subclasses of thePublicof members,ProtectedMembers of the group are also directly subclasses of theprotected The member. java protected privateprivate " are still present in the subclass, but not directly accessible in subclasses. We can not redefine the access properties of the inherited members in the child class. If we try to redefine a private member variable that already exists in the parent class, then we are defining a variable that is completely unrelated to the member variable of the parent class, in which we can access the variable defined in the subclass and access the parent class in the method of the parent class. Although they have the same name but do not affect each other. If we try to define a non-private member variable that already exists in the parent class, the compiler will give us a compile error.

When constructing an object of a subclass, the constructor method of the parent class is also called, and the constructor of the parent class is called before the child class is constructed. During a program's run, a subset of the child object's space holds the parent class object. Because subclasses inherit from the parent class, members of the parent class may be used during the initialization of the subclass object. So the space of the parent class is initialized first, and then the space of the subclass is initialized. In this process, it is important to pass parameters if the constructor method of the parent class requires parameters.

The relationship between the Java subclass and the parent class

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.