Java Fundamentals Section----3. Inheritance, subclass instantiation, function replication

Source: Internet
Author: User

1. Inheritance

Java only supports single inheritance, and a subclass can inherit only one parent class. A parent class can have more than one child class

Keyword: extends

Reduce duplicate code

2. The grammatical characteristics of inheritance

Class Student extends preson{

}

If the subclass inherits the parent class, and the child class adds its own member variable and member function on this basis

3. Instantiation of Subclasses

1. Procedure for generating subclasses

In the constructor of a subclass, the constructor of the parent class must be called (depending on the number of arguments passed in () and the type determined)

extends person{    int  grade;        Student () {        Super();        System.out.println ("student constructor with no arguments");    }    Student (int  grade) {        this();    }}

Super (); If you do not write, the compiler will add this sentence for you;

2. Calling the constructor of the parent class using Super

4. Replication of functions (override)

This (parameter) calls the constructor in this class this (). method, calling member functions in this class

Super (parameter) calls the constructor of the parent class ..... Ibid.

Replication is also known as overwriting or rewriting

1. Among the two classes that have a parent-child relationship

2. The parent class and subclass each have a function, the definitions of the two functions (return value type, function name and parameter list) are identical

5. Use super to call member functions in the parent class

Overloading is in a class, the replication is in two classes, with Super call the parent class function, and then add their own code;

Java Fundamentals Section----3. Inheritance, subclass instantiation, function replication

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.