What is inheritance?
In object orientation, inheritance is a class that obtains the member variables and member methods of another class.
Java only supports single inheritance and does not allow many inheritance
Inheritance is used to reduce repeated code and facilitate modification.
Example:
Parent Person3
Class Person3 {String name; int age; void eat () {System. out. print ("dinner");} void introduce () {System. out. print ("My name is" + name + ", my age is" + age );}}
Subclass Student
Class Student extends Person3 {// extends inheritance; the extended Student subclass obtains the member functions and member variables in the parent class of Person3. // The subclass can be added with its own unique member variables and member functions int grade; void study () {System. out. print ("Learning ");}}
Main Function
Class TextStudent {public static void main (String args []) {Student student = new Student (); student. name = "Li Hua"; student. age = 19; student. eat (); student. introduce (); student. study ();}}
Output result:
Dinner
My name is Li Hua, and my age is 19
Learning