Java uses the extends keyword to declare that a class inherits from another class.
class classextends parent class {}
Example:
Public parent class
Animal {
String name;
ID;
Animal MyName myID) {
name MyName;
ID myID;
}
Introduction (){
System.out.println ("hello,everyone! My name is "+ ID +", "+ Name +". ");
}
}
// sub-class Public class extends Animal { publicint myid) { super(MyName, myID);} } Publicclassextends Animal { publicint myID) { super(MyName, myID); }}
Inherited attributes:
Subclasses have properties that are not private to the parent class, methods.
Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
Subclasses can implement methods of the parent class in their own way.
Java inheritance is a single inheritance, but can be multiple inheritance, single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so the Class C is the parent class of Class B, and Class B is the parent class of Class A, which is a feature that differs from C + + inheritance.
Increases the coupling between classes (the disadvantage of inheritance, high coupling will cause the relationship between the code).
Inheritance keywords:
- Extends keyword ( single inheritance, a subclass can have only one parent )
- Implements keywords (using the Implements keyword can be disguised to make Java have multiple inheritance, using the scope of the class to inherit the interface, you can inherit multiple interfaces (interfaces and interfaces separated by commas).
// Implements keyword example Public Interface A { publicvoid eat (); Public void public interface B { publicvoid Public class Implements A, b {}
- Super keyword (we can use the Super keyword to implement access to a parent class member to refer to the parent class of the current object.) )
- The This keyword, which points to its own reference. )
class Superclass { int i =classextends superclass { int i =100; Public void ShowMessage () { System.out.printf (Superthis. i); }}
Output:
Super this. i = 100
The final keyword declares that a class can define a class as not inheritable, that is, a final class, or for a decorated method that cannot be overridden by a quilt class:
Declaring a class:
Final class Name {// class Body}
Declaration method:
Modifier (Public,private,voidfinal return value type method name () {// method Body}
A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list.
If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.
Java Object-oriented