Java high-frequency words--inheritance

Source: Internet
Author: User

Personal notes.

The three giants encapsulate inheritance and polymorphism, and inheritance as one of them, its importance is self-evident. The main points of inheritance comparison:

1. Basics of Inheritance: Subclasses use the extends keyword to inherit the non-private properties and methods of the parent class.

2. Benefits of Inheritance: Once a parent class is created, the common characteristics of a series of subclass objects are defined.

3. Subclass calls the parent class method using the Super keyword.

Inheritance of constructors:

Both the parent and child classes have their own constructors, but what is the constructor responsible for loading the subclass object? Is it the responsibility of the parent class or the child class? The answer is: the parent class is responsible for constructing the parent class part, and the child class constructs the subclass part. Let's do an experiment.

classshape{inth; intW;  Publicshape () {System.out.println ("Super-shape"); }    }classTriangleextendsshape{intLineNum;  PublicTriangleintWintHintlinenum) {         This. LineNum =LineNum; System.out.println (H+w+linenum); }    } Public classExtendstest { Public Static voidMain (string[] args) {triangle T=NewTriangle (5,4,3); }}

Output Result:

Super-shape

12

proves that the constructor method of the parent class is loaded.

Java high-frequency words--inheritance

Related Article

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.