This article discusses when to use abstract classes and interfaces.
Abstract class ):
We know that Java object-oriented programming has the concept of inheritance. When some parent classes do not want to create their instances, we need to use abstract classes. For example, if the parent class of a triangle, circle, or square is "shape", we can create an instance of triangle, circle, and square, but we do not want to create an instance of "shape, because the shape does not exist, an abstract method is used to define the shape ".
abstract class Shape{ abstract void draw(); abstract void erase();}
It is worth noting that if a class has an abstract method, this class must be declared as an abstract class. If the parent class is an abstract class, the subclass must overwrite all abstract methods in the parent class. Otherwise, the subclass will become an abstract class.
Abstract classes are designed to avoid creating objects of this class.
Interface ):
Java further promotes the abstract concept. This is the interface, and the interface is actually a completely abstract class. We know that the difference between an interface and an abstract class is that all the methods in the interface have no method bodies and are all public abstract types. All the member variables in the interface are public static final variables, and must be initialized by definition.
interface Shape{ void draw(); void erase();}
OK. The abstract class and interface have been briefly introduced. I think most people are sure to be the same as me and still do not understand the differences between the two!
Here is a summary:
1. abstract classes and interfaces can indeed be replaced with each other to some extent, for example, in the above example.
2. abstract classes can only be inherited, and only one subclass can be inherited, while many interfaces can be implemented.
3. How to choose to use an abstract class or interface depends on the program design. The following is an example:
We want to implement a "Door". Its sub-classes include wooden doors, iron doors, and glass doors:
We use abstract classes to implement:
abstract class Door{ abstract void open(); abstract void close();}
You can also use the following interface:
interface Door{ void open(); void close();}
Now the boss suggested that we should produce anti-theft doors and install them on iron doors and glass doors.
At this time, there will be an alarm () method, but it is clear that the implementation of the alarm method is unreasonable in the door abstract class, because not all doors have this method.
It is not appropriate to add the alarm () method to each subclass. Therefore, you should use the interface at this time. Let's redesign the program:
abstract class Door{ abstract void open(); abstract void close();}
interface Alarm { void alarm();}
class AlarmDoor extends Door implements Alarm { void open() { … } void close() { … } void alarm() { … }}
This is perfect. If the boss asks us to add the Cat's Eye function to the wooden doors and iron doors, we only need to design a cat's eye interface.
Do you know when to use abstract classes and interfaces?