Inheritance is the relationship between classes and classes, and is a very simple and intuitive concept, similar to inheritance in the real world (such as a son inheriting a father's property).
Inheritance can be understood as a procedure for a class to get methods and properties from another class. If Class B inherits from Class A, then B has the method and properties of a.
Inheritance uses the extends keyword.
For example, we have defined a class people:
- Class people{
- String name;
- int age;
- int height;
- void say(){
- System. Out. println("My name is" + name + ", age is" + ages + ", height is" + height ");
- }
- }
If you need to define a class Teacher now, it also has the name, age, Height property, and say () method, and you need to add school, seniority, subject properties, and lecturing () methods. Do we need to redefine a class?
Completely unnecessary, you can first inherit the members of the People class, and then add your own members, for example:
- Class Teacher extends people{
- String School; //Your school
- String subject; //Disciplines
- int seniority; //seniority
- //overwrite the Say () method in the People class
- void say(){
- System. Out. println("I call" + name + "," + school + "teach" + subject + ", have" + Seniorit Y + "Years of seniority");
- }
- void lecturing(){
- System. Out. println("I have" + Age + "old, still stand on the podium lectures");
- }
- }
Description of the program
- The name and age variables, although not defined in Teacher, are defined in people and can be used directly.
- Teacher is a subclass of people, people is the parent class of the Teacher class.
- Subclasses can override methods of the parent class.
- Subclasses can inherit all members of the parent class except private.
- The construction method cannot be inherited.
Inheritance is a great step forward in terms of maintenance and reliability. If you make modifications in the People class, the Teacher class is automatically modified without requiring the programmer to do any work, except to compile it.
Single inheritance: Java allows a class to inherit only one other class, that is, a class can have only one parent class, and this restriction is called single inheritance. The concept of Interface (interface) is learned later, and interfaces allow multiple inheritance.
Finally, the above code is organized:
- Public class Demo {
- public static void main(String[] args) {
- Teacher t = new Teacher();
- T. Name = "cloth";
- T. Age = ;
- T. School = "Tsinghua University";
- T. Subject = "Java";
- T. Seniority = n;
- T. Say();
- T. Lecturing();
- }
- }
- Class people{
- String name;
- int age;
- int height;
- void say(){
- System. Out. println("My name is" + name + ", age is" + ages + ", height is" + height ");
- }
- }
- Class Teacher extends people{
- String School; //Your school
- String subject; //Disciplines
- int seniority; //seniority
- //overwrite the Say () method in the People class
- void say(){
- System. Out. println("I call" + name + "," + school + "teach" + subject + ", have" + Seniorit Y + "Years of seniority");
- }
- void lecturing(){
- System. Out. println("I have" + Age + "old, still stand on the podium lectures");
- }
- }
Operation Result:
My name is Xiao, teaching Java at Tsinghua University, with 12 years of seniority
I'm 70 years old, still standing on the podium and lecturing.
Note: The construction method cannot be inherited, it is important to master this. A class can be constructed with only two methods: writing a constructor method, or not having a constructor at all, and the class has a default constructor.
The concept and implementation of 2.Java inheritance