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)!

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...

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. ...

mybatis-plus...

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? Using EasyExcel for Excel...

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 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? Newbie who are just starting to learn back-end development often feel confused about choosing project management tools. Special...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment