Java Basics in mind

Source: Internet
Author: User

1. Can the Java subclass have the same property name as the parent class?

In Java, the attribute name in a subclass is the same as a property name of the parent class, so it is said that this property of the subclass hides the property of the parent class. Instances of subclasses are accessed by their own, and the parent class is not affected. Subclass if you want to access a property with the same name as the parent class, add super

2. does the Java subclass inherit the private properties and methods of the parent class?

Subclasses inherit the parent class, the subclass has all the properties and methods of the parent class, and then use the program to verify that the parent class's private properties and private methods, the subclass is inaccessible, of course, some of the parent class's private properties may be accessible through the appropriate method, but the private method does not seem to be easy to access (this is not considered Java reflection mechanism) so I simply think that the subclass cannot inherit the parent class's private properties and methods, but after analyzing the memory, I found that I was wrong , when a subclass was created, first a parent class object was created in memory, and then in the parent The class object is placed outside the attributes that are unique to the subclass, which together form the object of a child class. So so-called inheritance enables subclasses to have all the properties and methods of the parent class: The subclass Object does have all the properties and methods in the parent class object, but the private properties and methods in the parent class object are inaccessible to the child class, but they are owned but not used. It's like something you might have, but you can't use it. So the child class object is absolutely larger than the parent class object, so-called subclass objects can only inherit the parent non-private properties and methods of the argument is wrong. Can inherit, just can't access it.

This is some of the most basic and self-ambiguous Java foundations encountered in learning and life, and will be added in real-time, that is, continuous updating.

Java Basics in mind

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.