Java Chapter 17th Inheritance

Source: Internet
Author: User

The concept of inheritance

The inheritance mechanism is an indispensable key concept of object-oriented programming, and it is the foundation to realize software reusability.

is the main way to improve the expansibility and maintainability of software system.

The so-called inheritance refers to the definition of a class can be based on another already existing class, that is, the subclass is based on the parent class, thus implementing

The reuse of the parent code, the subclass "can absorb the data properties and behaviors of the existing classes, and can extend the new capabilities." ”

Use inheritance;

Writing the parent class: Class pet{//Public properties and methods}

To write a subclass, inherit the parent class:

Class Dog extends pet (can inherit only one parent) {//subclass-specific properties and methods}

Class Penguin extends (inheritance keyword) Pet {}

Subclass access to Parent class members

Access the Parent class construction method (use Super keyword Super to represent the parent class object)

Super ();

Super (name); ---> called in the Subclass constructor method and must be the first sentence

Accessing parent class Properties

Super.name;

Accessing the Parent class method

Super.print ();

Can subclasses inherit all the resources of the parent class?

Members of the parent class that cannot be inherited:

1. Private members

2. Subclasses are not in the same package as the parent class, members that use the default access permissions,

3. Construction method

Note: Cases that cannot be inherited

Access modifier protected

Properties and methods can be modified

This class, the same package, the subclass can access

Summary of access modifiers:

What is the initialization order of multiple inheritance relationships?

Parent class---parent class construction Method--child class---child class construction method

Note: Breakpoint Tracing initialization process

When to use inheritance?

Inheritance is similar to the real world

Just say "cat is a mammal", the cat's many properties, behavior is self-evident

Tibetan Mastiff is a kind of dog

Designs that conform to is-a relationships use inheritance

Inheritance is a way of code reuse

Putting properties and behaviors common to subclasses in a parent class

Method overrides:

Summary 1

Will the construction method also be rewritten? ---> Cannot be inherited, so cannot be rewritten!

Rules for method overrides

Method name is the same

Same argument list

The return value type is the same or its child class;

Access permissions cannot be strict with the parent class

Method overloading and method rewriting

Summary 2

Super keyword to access members of the parent class

Super can only appear in the methods and construction methods of subclasses

Super calls the constructor method, only the first sentence

Super cannot access the private members of the parent class

Training essentials:

Inheriting, subclasses overriding the parent class method, understanding the initialization process in inheritance

Java Chapter 17th 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.