1. What is inheritance
Creates a new class based on a class that already exists. The existing class is the parent class, the new class is the subclass, the inheritance is the subclass inherits and owns the properties and methods of the parent class, and the child class has properties and methods that the parent class does not have.
A parent class, also known as a base class, a superclass (superclass), or a subclass, also known as a derived class.
2. Features of "Inheritance" in Java
A class in Java can inherit only one parent class. Unlike languages such as C + +, you can inherit multiple classes. This is one of the more easy-to-learn Java
Only non-private member properties and methods of the parent class can be inherited, private is unique to the parent class and cannot be inherited
3. The "inherited" format in Java.
"Inherit" using the keyword extends
class parent Class {} class extends parent Class {}
4. Example of "Inheritance":
PackageJavastudy;classPerson { Public Static voidMain (string[] args) {PersonCeshi2 pp=NewPersonCeshi2 ();//instantiating subclasses PersonCeshi2Pp.name= "Monkey King"; Pp.age=500; Pp.height=100; Pp.say (); }} Public classpersonceshi{//define parent class PersonceshiString name; intAge ; voidsay () {System.out.println ("Name:" +name+ "\ n Age:" +Age ); }}classPersonCeshi2extendspersonceshi{//define subclass PersonCeshi2, Inherit parent class Personceshi intHeight//properties specific to sub-categories voidSay () {//overrides the Say () method in the parent class in the subclass. Because there is no height property in the parent class, the Say () method overrides theSystem.out.println ("Name: +name+" \ n Age: "+age+" \ n Weight: "+height); }}
Output Result:
Name: Monkey King
Age: 500
Weight: 100
Note: Overriding the parent class in a subclass with the same name method, called the wrong "override", is also called "Overwrite".
A subclass has the same name method, the child class is called, and the child class is not, and the father is called.
Inheritance in Java