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
- public class person{
- private String name;
- Public person (String name) {
- this.name = name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- Public String GetName () {
- return name;
- }
- }
[HTML]View PlainCopy
- public class Student extends person{
- Public Student (String name) {
- Super (name);
- }
- }
[HTML]View PlainCopy
- public class teststudent{
- public static void Main (string[] args) {
- Student mstudent = new Student ("abc");
- String mname = mstudent.getname ();
- System.out.println ("Name is:" + mname);
- Mstudent.setname ("EFG");
- mname = mstudent.getname ();
- System.out.println ("Name is:" + mname);
- }
- }
Printing results are:
Name IS:ABC
Name is EFG
Does the Java subclass inherit the private properties and methods of the parent class?