As one of the three basic features of object-oriented, inheritance is also an essential part of Java. Therefore, the inheritance of classes in Java and other object-oriented languages is very similar.
Benefits of Inheritance:
- The definition of a class can be simplified by inheritance.
- Java only supports single inheritance and does not allow multiple inheritance.
- Can have multiple inheritance, that is, a class can inherit a subclass of a class, such as Class B inherits the Class A, Class C inherits the Class B, then C also indirectly inherited a.
- Subclasses inherit member variables and member methods of all parent classes, but do not inherit the constructor of the parent class. In the constructor of a subclass, you can use the statement super (argument list) to call the parent class's constructor method.
- If the constructor of a subclass does not have a dominant call to the constructor of the parent class, and no other constructor method is invoked with the This keyword, the system defaults to calling the parent class's constructor without arguments when the instance object of the subclass is generated.
To illustrate:
Declaration of a child class
1 class extends Parent class name 2 { 3 member variable 4 member function 5 }
Note: When the member variable of a subclass and the member variable of the parent class have the same name, there is a problem with variable overrides, and the override of the variable is related only to the variable name, and when the subclass executes a method that inherits from the parent class, the member variable of the parent class is processed
1 classCam02 { 3 DoubleSize=0; 4 voidprintsize ()5 { 6 System.out.println (size); 7 } 8 } 9 classCam1extendsCam0Ten { One DoubleSize=20; A } - Public classcam2 - { the Public Static voidMain (String args[]) - { -CAM1 a=Newcam1 (); - System.out.println (a.size); + a.printsize (); - } +}
Output to
20.00.0 when the member method of a subclass is the same as the prototype of a member method of a parent class, the problem of function overrides is not only related to the method name, but also to its parameter type, order, number, and return value. Note: Subclasses cannot inherit private methods of parent class
1 classCam02 { 3 intsize; 4 voidPrintSize (inta)5 { 6System.out.println ("AAA"); 7 } 8 } 9 classCam1extendsCam0Ten { One voidPrintSize (Doublea) A { -System.out.println ("BBB"); - } the } - Public classcam2 - { - Public Static voidMain (String args[]) + { -CAM1 a=Newcam1 (); +A.printsize (1); AA.printsize (1.0); at } -}
Output to
AAABBB because of the parameter type of the function, there is an overload here, the constructor inherits the subclass of a constructor that inherits the parent class without arguments, and then executes the constructor of the parent class by using the Super keyword in its own constructor to invoke the constructor of the parent class. The statement must appear in the first executable statement of a subclass constructor
1 classCam02 { 3 PublicCam0 ()4 { 5System.out.println ("AAA"); 6 } 7 } 8 classCam1extendsCam09 { Ten Publiccam1 () One { ASystem.out.println ("BBB"); - } - PublicCAM1 (inti) the { -SYSTEM.OUT.PRINTLN ("CCC"); - } www.2cto.com - } + Public classcam2 - { + Public Static voidMain (String args[]) A { atCAM1 a=Newcam1 (); -CAM1 b=NewCAM1 (1); - } -}
Output to
AAABBBAAACCC is the subclass that executes the constructor of the parent class before executing its own constructor.
On the inheritance of classes in Java