java-Preliminary Understanding-nineth-Abstract class-Overview

Source: Internet
Author: User

One.

There is also a part of inheritance called abstract class.

Why abstract classes are present, for instance.

, Demoa and demob two classes have the same method, and if there is no inheritance relationship between them, they can be extracted upward. After extraction, subclasses inherit the parent class. and the contents of the subclass can be omitted to write, the following is preserved, in fact, can be omitted.

This is what we said earlier about inheritance, discovering the relationship between things, and extracting commonalities upward. In this case, you can improve the reusability of the code.

But we have a problem: Demoa and demob do have the same function, but the content is different. We all know the function is generally in the package form, Demoa must have a display function, demob must have a display function, but the content is different. That means the function is divided into two parts, the first part called the declaration is consistent, the function body or function content is inconsistent. So we can make it clear that they also have commonalities, declarations, and features that are different, and that we can pull up because they have the same functionality.

We extracted the declaration of the method, but the content could not be drawn. The final form is as follows. It is called this kind of thing to have this function, but the content of this work does not know, because every concrete thing does not know. Since this function cannot be closed with curly braces and cannot be defined, it ends with a semicolon. The concluding sentence, either with curly braces or semicolons.

We watched the following, extracted from the content, really do not understand. Such a function must be identified, how to mark it? Be sure to use a keyword abstract, which is the meaning of abstraction.

Abstract keyword modifiers are called abstract functions. The class in which the abstract function resides must also be identified as abstract. This thing has a function. This function is what, how to do, do not know. Then this thing is not specific. This is the origin of the abstract class. (Is it necessary to extract this?) )

An example is given below to describe the abstract class. When we write here, we write from the bottom up, first we write the dog and the wolf, then we extract the same function. However, the actual development, is through the analysis, the first class canine, and then according to the specific contents of the function of the different, divided into dogs and wolves.

Function details can not be determined, is the abstract method, not a specific method. The canine family is not a specific thing.

Dogs and wolves inherit the canine family, and they can actually roar, all these specific things,

Some people will say that since the content of the function is not the same, then delete the canine branch, why also abstract. But in practice, in addition to yelling, there are other functions, such as sleeping, whether dogs or wolves, are lying on the bed. This is not an abstraction.

(now it's useful to say that the polymorphism will be known later.) )

Extraction and not extraction is a difference, extraction and no content, is not a subclass to achieve it. It's not the same when you use it. Let the dog bark and command the dog. Let the Wolf shout, the Wolf must be commanded .... Many kinds of direct command of the canine department roar, it is much easier.

The reason to tell an abstract class is to constantly extract it, so that it produces abstract things that require abstract classes.

java-Preliminary Understanding-nineth-Abstract class-Overview

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.