JAVA 11th (inheritance)

Source: Internet
Author: User

JAVA 11th (inheritance)
Inheritance: (extends)

Many classes, but some members are repeated. To provide reusability, duplicate code is extracted, and inheritance is applied.

1. Improved code reusability

2. Relationship between classes,Inheritance provides the premise for polymorphism, and there is no polymorphism without inheritance.


Class MAN {String name; int age;} class student extends MAN // MAN is the base class of student, and student is the subclass of MAN {void study () {System. out. println (name + "Learning" + age);} class teacher extends MAN {void work () {System. out. println (name + "working" + age) ;}} public class Main {public static void main (String [] args) {student BLF = new student (); BLF. name = "BLF2"; BLF. age = 20; BLF. study ();}}


Concept diagram:


Single inheritance and multi-Inheritance

JAVA supports single inheritance and does not directly support multi-inheritance. However, it is improved on the Multi-Inheritance Mechanism of C ++.

Definition: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Upload + upload/upload + PGJyPgo8L3A + CjxwPjxzdHJvbmc + PC9zdHJvbmc + PHByZSBjbGFzcz0 = "brush: java;"> # include Using namespace std; class A {public: void show () {cout <"sd" <

Multi-inheritance in java: Multi-inheritance is not directly supported, because once the same members exist in the parent class, uncertainty will occur. java improves the C ++ defect.


Java multi-inheritance is implemented through "Multi-Implementation ".

Java supports multi-layer (Multi-Level) Inheritance: D inherits C, C inherits B, and B inherits A, so the inheritance system appears,

Note:: To use an inheritance system:

1. view the top level of the system and understand the basic functions of the system.


2. Create the most subclass object of the system and use the function

Inheritance System diagram:



Unfinished ......


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.