Java access control and considerations arising from the combined inheritance system

Source: Internet
Author: User

Scope of the access modifier:

Private: You can define methods or properties that define methods and properties that cannot be accessed by external classes (including subclasses). Default: It can be accessed anywhere in the package. Protected: protection, non-subclasses in different packages cannot be accessed. (The members that can be accessed include the same package and any subclasses of the class) public: they are accessible and free of any restrictions.
Java Inheritance & access modifiers combined with the big mess 1. Does the subclass overloaded method have permission to be less than the parent class definition? Which, private<default<public not allowed,Note: The polymorphic representation of the parent Parent=new children () is allowed in Java, if the subclass method permission is allowed to be small at this timeinparent class, it is clear that the variable is declared as a child type and is allowed access when declared as parent, which is clearly illogical.
2.if the private method is declared in the parent class, is it overwrite to use the default permission in the subclass? is not overwrite and is now redefined.
3.overwrite of the parent class and the child class with the same name propertyReference Links:http://gaolixu.iteye.com/blog/363709
A. The same name variable access permission is the same:
The value of the variable depends on the type of the variable we define, not the type of the object being created (where the private modified variable is not considered), that is:
parent
parent=new Parent ();//The value
of the parent class Parent childofparent=new Child ();//The value of the parent class
child=new Children ();//The value of the subclass
B. The modification of the subclass access permission does not affect the final result.

Extended reading: Java's acquisition of class objects and the process analysis of instantiating objects in two ways


From for notes (Wiz)

Java access control and considerations arising from the combined inheritance system

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.