On the inheritance of classes in Java

Source: Internet
Author: User

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:

    1. The definition of a class can be simplified by inheritance.
    2. Java only supports single inheritance and does not allow multiple inheritance.
    3. 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.
    4. 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.
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.