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:
1 class people{ 2 String name; 3 Age; 4 Height; 5 void Say () { 7 System.out.println (" My name is "+ name +", age is "+ ages +", height is "+ height); 8 9 }
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:
1 classTeacherextendspeople{2String School;//Your school3String subject;//disciplines4 intseniority;//seniority5 6 //overriding the Say () method in the People class7 voidsay () {8SYSTEM.OUT.PRINTLN ("I call" + name + "," + School + "teaches" + Subject + ", has" + seniority + "Years of seniority");9 }Ten One voidlecturing () { ASystem.out.println ("I already" + Age + "old, still standing on the podium to lecture"); - } -}
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:
1 Public classDemo {2 Public Static voidMain (string[] args) {3Teacher T =NewTeacher ();4T.name = "the Cloth";5T.age = 70;6T.school = "Tsinghua University";7T.subject = "Java";8T.seniority = 12;9 T.say ();Ten t.lecturing (); One } A } - classpeople{ - String name; the intAge ; - intheight; - - voidsay () { +System.out.println ("My name is" + name + ", age is" + ages + ", height is" +height); - } + } A classTeacherextendspeople{ atString School;//Your school -String subject;//disciplines - intseniority;//seniority - - //overriding the Say () method in the People class - voidsay () { inSYSTEM.OUT.PRINTLN ("I call" + name + "," + School + "teaches" + Subject + ", has" + seniority + "Years of seniority"); - } to + voidlecturing () { -System.out.println ("I already" + Age + "old, still standing on the podium to lecture"); the } *}
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.
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (25) More about Java package
Java know how much (26) claim rules for source files
Java know how much (27) the concept and implementation of inheritance
Java know how much (27) the concept and implementation of inheritance