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.