Java-Preliminary Understanding-The Nineth chapter-the difference between an interface and an abstract class

Source: Internet
Author: User

I. The difference between an interface and an abstract class

Abstract class defines two methods, the interface can also define two methods, then at the time of definition, is the definition of abstract class or interface?

Inheritance and implementation are two different things.

Smoking is not the basic function of students, it is the extension function of students. If the student wants to smoke, it realizes the interface.

Class in determining the system, interfaces in determining the system's additional functionality.

For example,

According to the previously described knowledge, the program can write this, but we have made an improvement on it

What is the difference between the function of guiding blindness and the extraction of it? It seems to be easier not to extract them individually. But the thought is not that only the guide dog has the function of blindness, and other animals have this function.

I inherit the dog, inherit the basic function of the dog, inherit the guide blindness, it has the Guide function, but can not inherit more. Although the inheritance is not possible, but the interface can.

In this way, the two are clearly divided. This abstract class of dogs defines the most basic functions of a canine animal. Use classes to define basic functions, and in the area of problem analysis, blindness should be an extra function. The final form is shown below.

If the blind guide interface is not to be canceled, the guide is written to the guide dog, then the program is not extensible. Another point of view, if all the guide putschism can be set in the sub-class, Guide dogs, guide The Blind Cat, guide the Blind Pig. For all three to appear, to the next three orders, guide dogs on the Blind, guide the cat, on the Blind Pig. This brings the problem, there is common content, does not improve the reusability. Two, so to speak, all have guide blindness, all give me to go, so very simple answer. How the guide came from these species is constantly being extracted, which shows the benefits of extracting the guide function.

Let's talk about some of the analysis in the problem area.

Java-Preliminary Understanding-The Nineth chapter-the difference between an interface and an abstract class

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.