Concept:
Inheritance is the child class inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class's instance domain and method, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.
The Inheritance in life:
Rabbits and sheep belong to herbivores, and lions and leopards belong to carnivores.
Herbivores and carnivores belong to animals.
The parent class is more generic and the subclass more specific. Although herbivores and carnivores belong to animals, there are differences in their properties and behavior, so subclasses have their own characteristics as well as the general nature of the parent class.
The inheritance format of the class
In Java, the extends keyword can be used to declare that a class is inherited from another class in the following general form:
Class classextends parent class {// subclass inherits the parent class }
Why you need to inherit
Next we illustrate this requirement by example.
The development of animal species, in which animals are penguins and mice, are required as follows:
Penguin: Attribute (name, id), method (eat, sleep, introduce yourself)
Mouse: Attribute (name, id), method (eat, sleep, introduce yourself)
Public classPenguin {PrivateString name; Private intID; PublicPenguin (String MyName,intmyID) {Name=MyName; ID=myID; } Public voideat () {System.out.println (name+ "eating"); } Public voidsleep () {System.out.println (name+ "Sleeping"); } Public voidIntroduction () {SYSTEM.OUT.PRINTLN ("Hello, everyone!" I am "+ ID +" Number "+ Name +". "); } }
Public classMouse {//Mouse Type PrivateString name; Private intID; PublicMouse (String MyName,intmyID) {Name=MyName; ID=myID; } Public voideat () {System.out.println (name+ "eating"); } Public voidsleep () {System.out.println (name+ "Sleeping"); } Public voidIntroduction () {SYSTEM.OUT.PRINTLN ("Hello, everyone!" I am "+ ID +" Number "+ Name +". "); } }
From the two code can be seen, the code is duplicated, resulting in the result is a large and bloated code, and maintenance is not high (maintenance is mainly the latter need to modify the time, you need to modify a lot of code, error prone), so to fundamentally solve the problem of these two pieces of code, you need to inherit, Extract the same part of the two code to form a parent class:
Public classAnimal {//public parent class, animal class PrivateString name; Private intID; PublicAnimal (String MyName,intmyID) {Name=MyName; ID=myID; } Public voideat () {System.out.println (name+ "eating"); } Public voidsleep () {System.out.println (name+ "Sleeping"); } Public voidIntroduction () {SYSTEM.OUT.PRINTLN ("Hello, everyone!" I am "+ ID +" Number "+ Name +". "); } }
This animal class can be used as a parent class, and then the Penguin class and the mouse class inherit this class, there is the parent class in the properties and methods, the subclass will not have duplicate code, maintenance is also improved, the code is more concise, improve the reusability of code (reusability is mainly can be used multiple times, You don't have to write the same code more than once to inherit the code:
public class Penguin extends Animal { public
Penguin (String myName, int myID) { Span style= "COLOR: #0000ff" >super (MyName, myID); }}
Public class extends // extends Keywords Public int myID) { super/super keyword }}
Extends keyword: In Java, the inheritance of a class is a single inheritance, that is, a subclass can only have one parent class, so extends can inherit only one class. (A parent class can have more than one child class)
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.
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).
Constructors
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 generic 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.
Instance:
classSuperclass {Private intN; Superclass () {//class superclass constructors, which form function overloads when multiple occurrences occurSystem.out.println ("Superclass ()"); } Superclass (intN) {//class superclass constructors, which form function overloads when multiple occurrences occurSystem.out.println ("superclass (int n)"); This. N =N; }}classSubclassextendssuperclass{//class subclass inheriting class superclass Private intN; Subclass () {//class subclass constructors, which form function overloads when multiple occurrences occur Super(300); System.out.println ("Superclass"); } PublicSubclass (intN) {//class subclass constructors, which form function overloads when multiple occurrences occurSystem.out.println ("Subclass (int N):" +N); This. N =N; }} Public classtestsupersub{ Public Static voidMain (String args[]) {Subclass SC=Newsubclass (); Subclass SC2=NewSubclass (200); }}
The output is:
Superclass (int N) superclasssuperclass () subclass (int N): 200
The inheritance of Java object-oriented three major features