search
HomeJavajavaTutorial02.Java Basics - Inheritance

02.Java Basics - Inheritance

Feb 27, 2017 am 10:09 AM

Basic concepts

  • Concept of inheritance: Create a new class based on the type of an existing class, Without changing the form of the existing class, this approach is called inheritance.

  • The role of inheritance: After you first create a class P, you now need to create a new class S, but the function is similar to P. At this time, if we want to be lazy and don't want to rewrite S, we can do it through inheritance.

  • Inheritance relationship: The inheritance relationship is divided into parent class (base class) and child class (derived class). The parent class is the inherited object (such as P), and the subclass is the implemented inherited object (such as S).

  • Inheritance method: Class inheritance is Single inheritance, that is, after S inherits P, it cannot inherit P2.

  • Characteristics of inheritance: The subclass will automatically obtain all the fields (variables) and methods in the parent class through inheritance.

  • Implementation of inheritance: Inheritance is implemented in Java through the keyword extends.

class Parent {
}

class Son extends Parent {
}

IS-A relationship

The inheritance relationship in Java belongs to the IS-A relationship.

How to understand the IS-A relationship, take the above example: S inherits P, we can say that S is P.

In an inheritance relationship, the inheritor can completely replace the inheritee, but not vice versa. In a word, we "can treat people as animals, but we cannot treat animals as people".


Constructor and inheritance

A class is initialized by calling the constructor. For inheritance, the constructors of subclasses and parent classes have the following characteristics:

  • When the subclass inherits the parent class, if the constructor of the parent class is implicitly constructed Device, you don’t need to call it manually.

  • When a subclass inherits a parent class, if the constructor of the parent class is an explicit constructor, it must be called manually.

  • When a subclass inherits a parent class, the order of constructor calls is always from the parent class downwards.

Let’s explore it through an example:

class Parent {    // 无参构造器,即隐式构造器。
    public Parent() {
        System.out.println("initializing Parent");
    }
}

class Son extends Parent {    // 带参构造器,属显式构造器
    public Son(String name) {        // 关键 -> 由于父类是隐式构造器,这里可以不调用。
        System.out.println("initializing "+name);
    }
}

class Grandson extends Son {    public Grandson() {        // 关键 -> 父类是显式构造函数,必须手动调用
        super("son");
        System.out.println("initializing Grandson");
    }
}public class Test {
    public static void main(String[] args) {
        Grandson grandson = new Grandson();        // 关键 -> 构造器调用顺序总是从父类依次往下进行的,打印内容如下:
        // initializing Parent
        // initializing Son
        // initializing Grandson
    }
}

Basic concepts

  • Inheritance Concept : Create a new class based on the type of an existing class without changing the form of the existing class. This method is called inheritance.

  • The role of inheritance: After you first create a class P, you now need to create a new class S, but the function is similar to P. At this time, if we want to be lazy and don't want to rewrite S, we can do it through inheritance.

  • Inheritance relationship: The inheritance relationship is divided into parent class (base class) and child class (derived class). The parent class is the inherited object (such as P), and the subclass is the implemented inherited object (such as S).

  • Inheritance method: Class inheritance is Single inheritance, that is, after S inherits P, it cannot inherit P2.

  • Characteristics of inheritance: The subclass will automatically obtain all the fields (variables) and methods in the parent class through inheritance.

  • Implementation of inheritance: Inheritance is implemented in Java through the keyword extends.

class Parent {
}

class Son extends Parent {
}

IS-A relationship

The inheritance relationship in Java belongs to the IS-A relationship.

How to understand the IS-A relationship, take the above example: S inherits P, we can say that S is P.

In an inheritance relationship, the inheritor can completely replace the inheritee, but not vice versa. In a word, we "can treat people as animals, but we cannot treat animals as people".


Constructor and inheritance

A class is initialized by calling the constructor. For inheritance, the constructors of subclasses and parent classes have the following characteristics:

  • When the subclass inherits the parent class, if the constructor of the parent class is implicitly constructed Device, you don’t need to call it manually.

  • When a subclass inherits a parent class, if the constructor of the parent class is an explicit constructor, it must be called manually.

  • When a subclass inherits a parent class, the order of constructor calls is always from the parent class downwards.

Let’s explore it through an example:

class Parent {    // 无参构造器,即隐式构造器。
    public Parent() {
        System.out.println("initializing Parent");
    }
}

class Son extends Parent {    // 带参构造器,属显式构造器
    public Son(String name) {        // 关键 -> 由于父类是隐式构造器,这里可以不调用。
        System.out.println("initializing "+name);
    }
}

class Grandson extends Son {    public Grandson() {        // 关键 -> 父类是显式构造函数,必须手动调用
        super("son");
        System.out.println("initializing Grandson");
    }
}public class Test {
    public static void main(String[] args) {
        Grandson grandson = new Grandson();        // 关键 -> 构造器调用顺序总是从父类依次往下进行的,打印内容如下:
        // initializing Parent
        // initializing Son
        // initializing Grandson
    }
}

The above is the content of 02.Java Basics - Inheritance. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to properly configure apple-app-site-association file in pagoda nginx to avoid 404 errors?How to properly configure apple-app-site-association file in pagoda nginx to avoid 404 errors?Apr 19, 2025 pm 07:03 PM

How to correctly configure apple-app-site-association file in Baota nginx? Recently, the company's iOS department sent an apple-app-site-association file and...

What are the differences in the classification and implementation methods of the two consistency consensus algorithms?What are the differences in the classification and implementation methods of the two consistency consensus algorithms?Apr 19, 2025 pm 07:00 PM

How to understand the classification and implementation methods of two consistency consensus algorithms? At the protocol level, there has been no new members in the selection of consistency algorithms for many years. ...

What is the difference between IS TRUE and =True query conditions in MySQL?What is the difference between IS TRUE and =True query conditions in MySQL?Apr 19, 2025 pm 06:54 PM

The difference between ISTRUE and =True query conditions in MySQL In MySQL database, when processing Boolean values ​​(Booleans), ISTRUE and =TRUE...

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling?How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling?Apr 19, 2025 pm 06:51 PM

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling? Using EasyExcel for Excel...

As a Java programmer, how do you turn to audio and video development? What basic knowledge and resources do you need to learn?As a Java programmer, how do you turn to audio and video development? What basic knowledge and resources do you need to learn?Apr 19, 2025 pm 06:48 PM

How to switch from Java programmers to audio and video development? Learning Paths and Resources Recommendations If you are a Java programmer and are participating in a video project, �...

How to efficiently count the number of node services in MYSQL tree structure and ensure data consistency in Java?How to efficiently count the number of node services in MYSQL tree structure and ensure data consistency in Java?Apr 19, 2025 pm 06:45 PM

How to efficiently count the number of node services in MYSQL tree structure in Java? When using MYSQL database, how to count the number of nodes in the tree structure...

How do newcomers choose Java project management tools for backends: Maven or IntelliJ? Use the Maven that comes with IDEA or an additional download?How do newcomers choose Java project management tools for backends: Maven or IntelliJ? Use the Maven that comes with IDEA or an additional download?Apr 19, 2025 pm 06:42 PM

How do newcomers choose Java project management tools for backends? Newbie who are just starting to learn back-end development often feel confused about choosing project management tools. Special...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment