(Program this season:
This season explains the factors of inheritance in JAVA and the basic implementation of inheritance. Later, this season will discuss various limitations of inheritance in JAVA, including the instantiation process of subclass targets, method overriding, super keyword use, etc.
class inheritance
If the child inherits the father's business, if the child is not a prodigal, at least the family business can be undefeated. If the child is motivated, the family business will become more and more prosperous.
Investigate one of the following conditions:
· Person class: name attribute, age attribute, setter, getter
·Student class: name attribute, age attribute, school attribute, setter, getter
Student is a person, right? Compared with the Person class, Student has only one more School property. If it is defined as two classes, it must be wasteful, at least the name and age in the Student class are useless. We can manage the Student class as the Person class.
classPerson
{
Stringname;
intage;
publicStringgetInfo()
{
return name: this.name , age: this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
//If nothing is written here, it should be at least shared with the content of the Person class
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
// use subclass target
Students=newStudent();
//The following comprehensive operations are defined in the Person class
s.name=Wang Gan;
s.age=27;
System.out.println(s.getInfo());
}
};
Even if the subclass does not add any new tools, at least it can inherit a lot from the parent class. The following shows that the Student class inherits the Person class and calls the getInfo() method.
Then the subclass can actually extend the function of the parent class. At this time, it only needs to be defined directly in the subclass as before. Let's see Demo02 below.
classPerson
{
Stringname;
intage;
publicStringgetInfo()
{
return name: this.name , age: this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
//If nothing is written here, it should be at least shared with the content of the Person class
Stringschool;
};
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
// use subclass target
Students=newStudent();
//The following comprehensive operations are defined in the Person class
s.name=Wang Gan;
s.age=27;
//The school attribute added in the Student class
s.school=Jiangnan University;
System.out.println(s.getInfo() , campus: s.school);
}
};
Let's verify it, indeed, the drop-like function has been extended, which is the advantage of inheritance~
Inheritance rules
In real life, a child can only have one father, and a subclass can only have one parent. And cannot have multiple parent classes (in fact, this concept comes from C , C allows subclasses to have multiple parent classes~~~). But a parent class can have multiple subclasses~~~
Inheritance rules
When a subclass inherits, it directly inherits the non-private properties and non-private methods in the parent class, and implicitly inherits the private properties in the parent class (the subclass can access the parent class's private properties and private methods through the interface).
The old man=gt; the property is given to the child=gt; the child can open the safe through the old man's key~
=gt;love letter (confidential, private)=gt;safe
classPerson
{
privateStringname;
privateintage;
//Private properties can be accessed by participating in setter and getter operations~
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetInfo()
{
return name: this.name , age: this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
//If nothing is written here, it should be at least shared with the content of the Person class
Stringschool;
publicvoidfun()
{
setName(Wang Gan);
setAge(27);
}
publicvoidprint(){
System.out.println(getInfo() , campus: school);
}
};
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
// use subclass target
Students=newStudent();
}
};
We found that there is no error, through the setter and getter methods, the subclass can inherit the private properties name and age of the parent class
Private properties can be obtained directly through setter and getter methods in subclasses, so the private content should actually provide an exit for operations.
subclass target instantiation process
Let's verify whether the subclass target first calls the no-parameter structure method of the parent class when instantiating it, and then calls the structure method of the subclass. Let's look at Demo04
classPerson
{
privateStringname;
privateintage;
publicPerson()
{
System.out.println(
The structure method of the parent class
);
}
//Private properties can be accessed by participating in setter and getter operations~
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetInfo()
{
return name: this.name , age: this.age;
}
};
//The comparison between the Student class and the Person class extends the function of the Person class, so it can be inherited here
classStudentextendsPerson
{
publicStudent()
{
System.out.println(
Subclass structure method
);
}
//If nothing is written here, it should be at least shared with the content of the Person class
Stringschool;
publicvoidfun()
{
setName(Wang Gan);
setAge(27);
[Zero-based JAVA] JavaSE target-oriented part-13. Target-oriented high-grade (01).doc)