Does the Java subclass inherit the private properties and methods of the parent class?

Source: Internet
Author: User
Tags instance method

Recently learned to inherit, from the book to see the subclass inherit the parent class, the subclass has all the parent class properties and methods, 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's private properties may be accessible through the appropriate method, but the private method does not seem easy to access , the Java reflection mechanism is not considered here, so I analyze that the subclass can not inherit the parent private properties and methods, but after analyzing the memory, I found that I was wrong, when a subclass is created, first in memory to create a parent class object, and then put the child class unique properties outside the parent class object. The two combine to form an object of a subclass. So so-called inheritance enables subclasses to have all of the properties and methods of the parent class, so that the subclass object does have all the properties and methods in the parent class object, but the private properties and methods in the parent 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.

When a child class overrides a member variable of a parent class, the parent class method uses the member variable of the parent class, and the subclass method uses the member variable of the child class

(1) The subclass overrides the parent class's method, must have the same parameter return type, otherwise the compilation cannot pass through
(2) Subclasses override methods of the parent class, after jdk1.5, the parameter return class can be a subclass of the parent class method return class
(3) Subclasses override the parent class method, you can modify the method scope modifier, but you can only enlarge the scope of the method, and cannot change public to private
(4) Subclass method has access to the protected scope member of the parent class and cannot access the default scope member
(5) A static method of a subclass cannot hide a parent class instance method with the same name

Cannot inherit, subclasses can only write a public getxxx method in the parent class to get the private property in the parent class, and the subclass calls the getxxx of the parent class to get the private property

The public methods and domains (attributes) in the parent class inherit the quilt class in class inheritance, but the private will not be inherited.

So how do you inherit the child class to inherit the private domain of the parent class?

The answer is: the constructor method of the parent class is called through the Super () method in the constructor of the subclass.

That is, when you construct a subclass, you construct the same domain as the parent class for the child class. So in the object of the subclass, you also have the domain declared by the parent class.

And we should do the same.

[HTML]View PlainCopy
  1. public class person{
  2. private String name;
  3. Public person (String name) {
  4. this.name = name;
  5. }
  6. public void SetName (String name) {
  7. this.name = name;
  8. }
  9. Public String GetName () {
  10. return name;
  11. }
  12. }
[HTML]View PlainCopy
    1. public class Student extends person{
    2. Public Student (String name) {
    3. Super (name);
    4. }
    5. }


[HTML]View PlainCopy
  1. public class teststudent{
  2. public static void Main (string[] args) {
  3. Student mstudent = new Student ("abc");
  4. String mname = mstudent.getname ();
  5. System.out.println ("Name is:" + mname);
  6. Mstudent.setname ("EFG");
  7. mname = mstudent.getname ();
  8. System.out.println ("Name is:" + mname);
  9. }
  10. }

Printing results are:

Name IS:ABC

Name is EFG

Does the Java subclass inherit the private properties and methods of 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.