What is java inheritance
Inheritance is the most significant feature of object-oriented. Inheritance is the derivation of a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can expand new capabilities.
Java inheritance is a technology that uses the definition of an existing class as a basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but You cannot selectively inherit from parent classes. This technology makes it very easy to reuse previous code, which can greatly shorten the development cycle and reduce development costs. (Recommended tutorial: java tutorial)
For example, you can first define a class called a car. The car has the following attributes: body size, color, steering wheel, tires, and the car This class derives two classes, car and truck, adding a small trunk to the car and a large cargo box to the truck.
java inheritance characteristics:
(1) The inheritance relationship is transitive. If class C inherits class B, and class B inherits class A (multiple inheritance), then class C has not only the properties and methods inherited from class B, but also the properties and methods inherited from class A, and can also have its own new properties and methods. Defined properties and methods. Inherited properties and methods, although implicit, are still properties and methods of class C. Inheritance is the most effective way to construct, establish and extend new classes based on some more general classes.
(2) Inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship between related classes.
(3) Inheritance provides software reuse function. If class B inherits class A, then when creating class B, you only need to describe a few features (data members and member methods) that are different from the base class (class A). This approach can reduce the redundancy of code and data and greatly increase the reusability of programs.
(4) Inheritance reduces the interfaces and interfaces between modules by enhancing consistency, which greatly increases the maintainability of the program.
(5) Provide multiple inheritance mechanism. Theoretically, a class can be a special class of multiple general classes. It can inherit properties and methods from multiple general classes. This is multiple inheritance. For security and reliability reasons, Java only supports single inheritance and implements multiple inheritance by using the interface mechanism.
Example:
Parent class Person
##
/** * 父类 人类 * * @author zkj * */ public class Person { // 名字 protected String name; // 编号 protected int id; // 工作环境 protected String environment; // 特长 protected String speciality; public Person(String myName, int myid, String myEnvironment, String mySpeciality) { name = myName; id = myid; environment = myEnvironment; speciality = mySpeciality; } /* * 工作地点 */ public void work() { System.out.println(name + "在" + environment + "工作" + '。'); } /* * 工作能力 */ public void ability() { System.out.println(name + "我会" + speciality + '。'); } /* * 自我介绍 */ public void introduction() { System.out.println("大家好!我是" + id + "号" + name + '。'); } }Subclass:
package com.zkj.person; /** * 人类的子类 医生类 * * @author zkj * */ public class Doctor extends Person { public Doctor(String myName, int myid, String myEnvironment, String mySpeciality) { super(myName, myid, myEnvironment, mySpeciality); } /* * 自我介绍 */ void introductionMy() { super.introduction(); } /* * 自我介绍 覆写 */ public void introduction() { System.out.println("大家好!我是一名医生"); } /* * 工作职责 */ public void duty() { System.out.println("治病救人是我的职责"); } }Run:
public class Run { public static void main(String[] args) { // 医生张三 Doctor doctor = new Doctor("张三", 1, "医院", "诊断"); // 张三的自我介绍 doctor.introduction(); // 张三的工作地点 doctor.work(); // 张三的能力 doctor.ability(); // 张三的职责 doctor.duty(); } }
The above is the detailed content of what is java inheritance. For more information, please follow other related articles on the PHP Chinese website!

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft