Object oriented Java inheritance
1, Inheritance is that a subclass inherits the methods and properties of its parent class, which makes the code more concise and improves the reusability of the code
//Dogs public class Dog { private String color; private String name; private int price; private int age; public Dog(String color, String name, int price, int age) { this.color = color; this.name = name; this.price = price; this.age = age; } public void eat(){ System.out.println(name+"I am eating"); } public void sleep(){ System.out.println(name+"Sleeping"); } public void introduction() { System.out.println("dog{" + "Color:'" + color + '\'' + ", varieties:'" + name + '\'' + ", Price=" + price + ", Age=" + age + '}'); } }
//Public parent class public class Common { private String color; private String name; private int price; private int age; public Common(String color, String name, int price, int age) { this.color = color; this.name = name; this.price = price; this.age = age; } public void eat(){ System.out.println(name+"I am eating"); } public void sleep(){ System.out.println(name+"Sleeping"); } public void introduction() { System.out.println("dog{" + "Color:'" + color + '\'' + ", varieties:'" + name + '\'' + ", Price:=" + price + ", Age:=" + age + '}'); } }
//Cat class inherits the methods and properties of its parent class public class Cat extends Common{ public Cat(String color, String name, int price, int age){ super(color,name,price,age); } }
2, Inheritance type:
It should be noted that Java does not support multiple inheritance, but supports multiple inheritance.
3, Inherited properties
Subclasses have non private properties and methods of the parent class.
Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
(equivalent to innovation, the subclass is an independent individual, and the parent class is a fixed library, which is all common things)
Subclasses can implement the methods of their parent classes in their own way.
It improves the coupling between classes (the disadvantage of inheritance, the higher the coupling will cause the closer the connection between codes, the worse the code independence).
4, Inherit keywords
(1) extends keyword
Extensions can only inherit one class
(2) implements keyword
The implements keyword can make java have the feature of multi inheritance in a disguised form. When the scope of use is class inheritance of interfaces, you can inherit multiple interfaces at the same time (the interfaces are separated by commas).
5, super and this keywords
Super keyword: we can access the members of the parent class through the super keyword, which is used to reference the parent class of the current object.
this keyword: refers to your own reference.
The this keyword can be used to
The this keyword can be used to:
(1) Call the constructor of the parent class;
super(); or super(parameter list);
(2) Call the method of the parent class (when the child class overrides the method of the parent class);
super.Method name(parameter list);
(1) Call the constructor of the current class, and it must be the first statement of the method. For example: this(); Call the default constructor. This (parameter); Call the construction method with parameters.
(2) Defines the data field variable of the current object. It is generally used when the local variable in the method has the same name as the data field variable of the object. For example, this.num = num. This.num represents the data field variable num of the current object, and num represents the local variable in the method.
6, final keyword
Final keyword declares that a class can be defined as non inheritable, that is, the final class, the modified methods cannot be overridden, and the modified variables cannot be modified
7, Constructor
A subclass does not inherit the constructor (constructor or constructor) of the parent class. It is just a call (implicit or explicit). If the constructor of the parent class has parameters, the constructor of the parent class must be explicitly called through the super keyword in the constructor of the child class with the appropriate parameter list.
If the parent constructor has no parameters, it is not necessary to use the super keyword to call the parent constructor in the constructor of the child class, and the system will automatically call the parameterless constructor of the parent class.