Java Learning Lesson 11th (inheritance)

Source: Internet
Author: User

Inheritance: (extends)

Many classes, but some members, are duplicates, so to provide reusability, to extract the duplicated code, apply the inheritance

1. Improved reusability of the code

2. Let the class and the class have a relationship, inherit to the polymorphism provides the premise, no inheritance is not polymorphic



Concept Understanding diagram:


Single inheritance and multiple inheritance

Java supports single inheritance and does not directly support multiple inheritance, but it is improved on the multiple inheritance mechanism of C + +.

Defined:


1, single inheritance: A subclass can have only one direct parent class.

Class A extends B, b extends C;

2, multiple inheritance: A subclass can have multiple direct parent classes, (not allowed in Java) has been improved.

Class A extends B; A extend C;

Advantages of multiple Inheritance:

There are some variables in class A, there are some things in class B, if C inherits A and B, then C has a B

Disadvantages of C + + multiple inheritance:

See Code:

#include <iostream>using namespace Std;class a{public:void Show () {cout<< "SD" <<ENDL;} Class B{public:void Show () {cout<< "SSSD" <<endl;}} Class C:p ublic a,public b//a,b Show is the same function name {}c Blf;int main () {blf.show ();//The Multiple inheritance of C + + code here, there will be confusion, you must let Show,show1, Show2, Name guarantee is not the same, can return 0;}


Java Multi-Inheritance: does not directly support multiple inheritance, because once the same member in the parent class, there will be uncertainty, Java improved the C + + this flaw


Java's multi-inheritance is achieved through the "multi-implementation" approach.

Java supports multilayer (multiple) Inheritance: D Inherits C,c Inheritance B,b inherits a, the inheritance system appears,

Note : When you want to use an inheritance system:

1. Review the top level of the system to understand the basic functions of the system


2. Create the most sub-class object of the system, complete the use of the function

Inheritance system diagram:



Not finished ...


Java Learning Lesson 11th (inheritance)

Related Article

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.