Primary design: When does Java inheritance work? How to use it?

Source: Internet
Author: User

writer:bysocket (mud and brick pulp carpenter)

I. Review of Succession

the following are common:

1, dependence ("Uses-a")

2, Aggregation ("Has-a")

3, Inheritance ("Is-a") the relationship between the class

This is the UML class diagram Common three kinds of relationships, in addition to the common implementation (interface and implementation of the relationship between the class), such as combination .


inheritance , that is, "is-a" relationship , is a kind of expression of Special and general relationship. For example, a woman (special) is a person (in general). The keyword extends indicates that the new class being constructed derives from an existing class.

1. A class that already exists is called a superclass , parent class , or base class .

2. The new class is called a subclass or a derived class .


sometimes look at someone else's source code design. For example, a common interface, followed by an abstract class implementation interface, and then inherit the various implementations of the abstract class:

Generally, the general behavior is written in the top- level interface , the abstract class implements the following methods and fields common to implement the class, the implementation of the respective functions .

But how do they actually work? For example, under what premise to use the inheritance, what scenario premise, the following is the summary after thinking: (Think, Write & Do)

Ii. When will the succession be used? How to use

That is , the inheritance of the design when used, what skills , what needs to be aware of the place.

1. Public methods and fields are placed in the base class (that is, the parent class)

This sentence may be controversial, too blowing Maotiuz or strict. In the case of women and men, such as the Name field and age can be placed above the base class, but the second characteristic of women is unique.

But sometimes the examples are always confusing: for example, Java engineer Intern and Java engineer, it seems that "intern" extends "Java Engineer", and then a lot of Java engineers on the fields are not belong to the Java intern. It's not too much of a relationship, it's probably all from a parent class-engineer. The following class diagram is correct:

The child class inherits from the parent class, including the public and protected methods and fields of the parent class. But subclasses only need to inherit part of the parent class. This time remember a word: " multi-use combination, less inheritance ."

2, protect does not protect the parent class

in fact, the protect mechanism in the parent class can not play a good protection. Subclasses can access the parent class when needed. But inheritance is unrestricted, that is, subclasses of subclasses ... Endless. If you want to hack into the parent class protect method, simply write a class, inheriting any subclass to be accessible. Both, under the same package can be accessed.

from the above can also be summed up:

3. When inheriting the methods and fields of the parent class, select Inherit. Otherwise, do not use inheritance. 4. Do not deviate from the original design method when overriding the method in the subclass.

A method implementation or definition of a parent class specifies the connotation of a behavior. Therefore, when inheriting the parent class, there is a rewrite (override) method that can change the behavior of the subclass. But please do not change the connotation of its definition. Common in the source code are: for example, the read write method in IO and the get post in the servlet.

5. Inheritance and combination, polymorphism

inheritance , subclasses and parent classes can determine their objects at compile time . combined or polymorphic, The object can be determined at run time , in contrast, the combination and polymorphism achieve more flexibility . However, errors that are unknown during the run time are to be handled with care.

gu, " multi-use combination, less inheritance ".

Iii. Summary of this paper

inherit the bit by bit. Mason, this soft paper summary, inevitably there are mistakes. Welcome to the discussion.

Welcome to click on my blog and github-blog to provide RSS Subscription Oh!

———- http://www.bysocket.com/ ————- https://github.com/JeffLi1993 ———-

Micro-Blog: Bysocket Watercress: Bysocket FaceBook: Bysocket Twitter: Bysocket

Primary design: When does Java inheritance work? How to use it?

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.