What is inheritance?
In object-oriented, inheritance is a class that obtains member variables and member methods in another class.
only single inheritance is supported in Java, multiple inheritance is not allowed
Inheritance is used to reduce duplicate code and is easy to modify
Example:
Parent class Person3
class person3{ String name; int Age ; void eat () { System.out.print ("eat"); } void introduce () { System.out.print ("My name is" +name+ ", my age is" + "); }
Sub-class Student
class extends person3{ //extends inheritance; extended student subclasses get member functions and member variables in the Person3 parent class / subclasses can add their own unique member variables and member functions int grade; void Study () { System.out.print ("learning");} }
Main function
class textstudent{ publicstaticvoid main (String args[]) { new Student (); Student.name = "Pear"; = +; Student.eat (); Student.introduce (); Student.study (); }}
Output Result:
Eat
My name is pear, my age is 19
Learn