Comparison between Java abstract class and interface

Comparison between Java abstract class and interface

preface

Abstract class and interface are both used by java to describe abstractions. I wonder if any students like me still have doubts about the grammatical differences between the two and how to choose them. Anyway, next, let me introduce abstract class and interface in detail.

Understanding abstract classes

In the concept of object-oriented, all objects are described by classes. But the reverse is not the case. Not all classes are used to describe objects. Because there may not be enough information in this class to describe a specific object, such a class is an abstract class (PS: note that the abstract class here does not only refer to abstract class). Abstract classes are often used to describe the abstract concepts obtained after we analyze and design the problem domain. They are abstractions of a series of concrete concepts that look different but are essentially the same.

Differences between abstract class and interface syntax

The differences between abstract classes and interfaces are as follows:

Differences between abstract class and interface

The design of abstract class reflects the "is-a" relationship, while the interface reflects the "has-a" relationship.

When to use the interface?

If you want to extend what I give you, you must implement the necessary interfaces. For example, objects that implement the comparable interface can directly use the sort method collections Sort (list) sort.

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. If your design is based on this class, you can inherit this abstract class and implement your own unique methods.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>