Java abstract class and interface comparison
Preface
abstract class and interface are both used by Java to describe abstract bodies. I wonder if any of my classmates are as confused as I do about the grammatical level of the two and how to choose between them. Anyway, let me introduce abstract class and interface in detail.
Understand abstract classes
In the object-oriented concept, all objects are described by classes. But this is not the case in reverse. Not all classes are used to describe objects. Because there may not be enough information in this class to describe a concrete object, such a class is an abstract class (ps: note that the abstract class here does not refer to abstract class alone). Abstract classes are often used to describe abstract concepts we obtain after analyzing and designing the problem area. They are abstractions of a series of concrete concepts that seem different but are essentially the same.
The difference between abstract class and interface syntax
The difference between abstract classes and interfaces is as follows:
The difference between abstract class and interface
The design of abstract class reflects the relationship of "is-a", while the interface reflects the relationship of "has-a".
When to use the interface?
If you want to extend what I give you, you must implement the necessary interface. For example, objects that implement the Comparable interface can be sorted directly using the sort method Collections.sort(List list).
When to use abstract classes?
If you have an abstract class, it provides many general functions and abstracts the methods that each subclass needs to implement by itself. What you design happens to be based on this class, then you can inherit this abstract class and implement your own unique method in this implementation.
Thank you for reading, I hope it can help you. Thank you for your support for this site!