Inheritance of Java object-oriented programming (i)

Source: Internet
Author: User
Tags instance method

Objective

The Java language has three important features, namely inheritance, polymorphism, and encapsulation. Today we're going to talk about inheritance, inheritance is a powerful means of reusing code, and using inheritance we will write a lot less code, or we can make the logic clearer when we write the code.

Basic syntax for inheritance

In the Java language, the "extends" keyword is used to denote that 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, and we say that the Sub class is a subclass of the base class, which is the parent class of the Sub class.

So what exactly does the sub class inherit from the base class? This requires a discussion of the situation:

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

The following program illustrates those member variables and methods that sub inherits from base in both cases:

// first create the parent class base  Public class base{    publicint//publicaccess level    privateint privatevarofbase = 1;  // Private access Level    int // default access Level    protected void methodofbase () {}  //Protected access level }

//Create sub-class sub to inherit parent class base Public classSubextendsbase{ Public voidmethodofsub () {publicvarofbase= 2;//valid, can be visited base of the public type of variablesDefaultvarofbase = 2;//valid, variable that can access the default access level of basePrivatevarofbase = 2;//illegal, cannot access a variable of base's private typemethodofbase ();//legal, method to access base's protected type    }     Public Static voidMain (string[] args) {Sub Sub=NewSub (); Sub.publicvarofbase= 3;//Legal, sub inherits a variable of the public type of baseSub.privatevarofbase = 3;//illegal, sub has no variable that inherits the private type of baseSub.defaultvarofbase = 3;//Legal, sub inherits the default access level of the base variablesub.methodofbase ();//Legal, sub inherits the method of base's protected typeSub.methodofsub ();//legal, which is the instance method of the sub itself    }}

The Java language does not support multiple inheritance, meaning that a class can only inherit another class directly. The reason here is that "one class directly inherits another class" is because the class can also have more than one indirect parent class, for example, the following code indicates that Class B inherits Class C, Class A inherits Class B, and Class C is the indirect parent of Class A:

 Public class extends b{...}  Public class extends C{...}

In addition, all classes in Java inherit the Java.lang.Object class directly or indirectly, which is the ancestor of all classes that define the same behavior that all Java objects have. Therefore, when defining a class, without using the "extends" keyword, you cannot hastily assume that the class does not inherit another class, but rather that the class inherits directly from the object class.

Inheritance of Java object-oriented programming (i)

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.