A deep analysis of the differences between Java interfaces and abstract classes _java

Source: Internet
Author: User

This paper analyzes the difference between interface and abstract class in Java learning, which is very important for beginners to learn and master the concept of Java programming. Details are as follows:

Java beginners are apt to ask the question: what is the difference between an interface and an abstract class? What do you choose to use the interface and abstract classes on?

Interfaces and abstract classes are not the same concept. An interface is an abstraction of an action, and an abstract class is an abstraction of the root cause .
An abstract class represents what this object is . The interface represents what the object can do . For example, men, women, these two categories (if it is class ...). ), their abstract class is human. show that they are all human.

People can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to achieve it.
So, in advanced languages, a class can only inherit a class (abstract class) (just as people cannot be both biological and non biological), but can implement multiple interfaces (eating interface, walking interface).

1th. interfaces are variants of abstract classes, and all methods in an interface are abstract . an abstract class is a class that declares the existence of a method and does not implement it .

2nd. the interface can inherit, the abstract class is not OK (Note: Here the original author is wrong, the abstract class can be inherited, if all of the subclasses implement the method in the abstract class, then the subclass is not an abstract class; Conversely, if one is not implemented, the subclass is also an abstract class)

3rd. An interface defines a method that cannot be implemented, while an abstract class can implement some of the methods.

4th. The base data type in the interface is static and the class image is not .

When you focus on the nature of a thing, use an abstract class; When you focus on an action, use an interface .

abstract classes are far more functional than interfaces, but the cost of defining abstract classes is high . Because of the advanced language (as well as the actual design), each class can inherit only one class. In this class, you must inherit or write all the commonalities of all of its subclasses. Although the interface is functionally weaker, it is only a description of an action. And you can implement multiple interfaces simultaneously in a class. In the design phase will reduce the difficulty.

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.