Home  >  Article  >  Java  >  what is java inheritance

what is java inheritance

angryTom
angryTomOriginal
2019-11-11 10:48:5610036browse

what is java inheritance

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!

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