Java Basic Learning Notes-inheritance

Source: Internet
Author: User

Inheritance is a powerful means of reusing program code when multiple classes (SUB1,SUB2,... SUB100), the parent class base can be abstracted from these classes when the same properties and methods exist between them. These same properties and methods are defined in the parent class base, and all sub classes do not need to redefine these properties and methods, but simply declare the inherited base class through the extends statement:

                 Public class extends Base {...}

The Sub class automatically has properties and methods defined in the base class.

6.1 Basic syntax for inheritance

  In the Java language, using the extends keyword to represent a class inherits another class, for example:

              public class sub extends base{...    }                                     

The above code indicates that the sub class inherits from the base class. So what exactly does the class inherit from the base class? This needs to be divided into the following two scenarios.

    • When the sub class and the base class are in the same package: The Sub class inherits the member variables and member methods of the public, protected, and default access levels in the base class.
    • When the sub class and the base class are in different packages: the Sub class inherits the member variables and member methods of the public and protected access levels in the base class.

Assuming that the sub and base classes are in the same package, the following program demonstrates which member variables and methods of the base class can inherit from the sub class.

         Public   classBase { Public intpublicvarofbase=1;//Public access Level                      Private intprivatevarofbase=1;//Public access Level                      intdefaultvarofbase=1;//Default Access Level                 protected voidMethodofbase () {}//Protected Access Level            }               Public classSubextendsbase{ Public voidmethodofbase () {publicvarofbase= 2;//valid, variable that can access the public type of the base classdefaultvarofbase=2;//valid, variable that can be the default access level of the base classprivatevarofbase=2;//illegal, cannot access a variable of the private type of the base classMethodofbase ();//A method that can access the protected type of the base class       }         Public Static voidMian (String args[]) {Sub Sub=NewSub (); Sub.publicvarofbase= 3;//Legal, the sub class inherits the variables of the public type of the base classsub.privatevarofbase=3;//illegal, a sub class cannot inherit a variable of the private type of the base classsub.defaultvarofbase=3;//Legal, Sub class inherits the default access level of the base class variableSub.methodofbase ();//Legal, Sub class inherits the method of the base class's protected typeSub.methodofsub ();//Legal, this is an instance method of the Sub class itself      } }

The Java language does not support multiple inheritance, meaning that a class can inherit only one class directly. For example, the following code causes a compilation error.

          class extends BASE1,BASE2,BASE3 (...)

Although a class can have only one direct parent class, it may have multiple indirect parent classes, such as the following code that indicates that the Base1 class inherits the Base2 class, the Sub class inherits the Base1 class, and the Base2 class is the indirect parent class of the Sub class.

                class extends base2{...}                 class extends Base1{...}

Therefore, the Java class inherits the Java.lang.Object class directly or indirectly.

6.2 Method Overloading (overload)

For a method of a class (including a method inherited from a parent class), if the method name of the two methods is the same, but the parameters are inconsistent, then one method is another overloaded method.

The overloaded method must meet the following conditions:

    • The method name is the same.
    • The method has at least one of the parameter types, number of people, and order.
    • The return type of the method can be different.
    • The modifiers for the method can be different.

In a class, it is not allowed to define two method names that are identical, and parameter signatures are identical. Because if there are two of these methods, the Java virtual machine cannot decide which method to execute at run time. The parameter signature refers to the type, number, and order of the parameters.

Java Basic Learning Notes-inheritance

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.