Does the Java neutron class inherit only the non-private variables and methods of the parent class? __java

Source: Internet
Author: User
Today, in the CSDN forum, I was asked if the Java Neutron class inherits only the non-private variables and methods of the parent class (originally posted here). Most of the comments are considered correct, but the underlying understanding of the problem is wrong.

First we make clear the concept of the term "inheritance", in Java, the meaning of the word inheritance is limited. A subclass can inherit only the accessible members of its parent class, and the subclass does not overwrite or hide the accessible members of the parent class. Therefore, a member of a class refers to the properties and methods declared in the class, plus the properties and methods inherited from their parent class. That is, subclasses cannot inherit private members of the parent class.

Although subclasses do not inherit private members from the parent class, these private members of life in the parent class are still part of the subclass type object. Because it is obviously not sufficient to initialize only the attributes declared in the current class when the object is instantiated, you also need to initialize all the declared properties in its parent class. During instantiation, the JVM needs to allocate space for all defined properties in the object's class and its parent class, including private members declared in the parent class.

So, we can say that a subclass cannot inherit private members from a parent class, but the object of a subclass is a private member that the subclass cannot inherit from the parent class.

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.