Differences and comparisons between interfaces and abstract classes in java

  • 2020-10-07 18:41:43
  • OfStack

Differences and comparisons between interfaces and abstract classes in java

The concepts of interfaces and abstract classes are not the same.

Interfaces are abstractions to actions, and abstract classes are abstractions to roots.

An abstract class represents what this object is. An interface represents what this object can do. For example, men and women, these two categories (if they are categories...) Their abstract class is people. That means they're all human.
People can eat, dogs can eat, you can define "eating" as an interface, and then let these classes implement it.

So, in a high-level language, a class can inherit from only one class (an abstract class) (just as people cannot be biological and non-biological at the same time), but multiple interfaces (eating interfaces, walking interfaces) can be implemented.

Point 1: An interface is a variant of an abstract class. All methods in an interface are abstract. An abstract class is a class that declares the existence of a method without implementing it.
Point 2: Interfaces can be multi-inherited, not abstract classes
Point 3: Interface definition methods cannot be implemented, whereas abstract classes can implement partial methods.
Point 4: The basic data type in the interface is static and the class image is not.

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

Abstract classes are much more powerful than interfaces, but defining abstract classes is expensive. Because in a high-level language (and by design) you can only inherit from one class per class. In this class, you must inherit or write all of its subclasses.

All the commonalities. Although the interface is much less functional, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. It will be easier during the design phase.

The above is the detailed explanation of interface and abstract class in java. If you have any questions, please leave a message or go to our community to exchange and discuss. Thank you for reading.


Related articles: