Abstract is one of the characteristics of object-oriented programming. In Java, OOP abstractions can be represented in two forms: interfaces and abstract classes. There are so many similarities and so many differences. Many people start out thinking they can be used interchangeably, but they are not. Today we are going to look at interfaces and abstract classes in Java.
1. Abstract classes
Before we look at abstract classes, let’s look at abstract methods. An abstract method is a special kind of method: it has declarations and no concrete implementation. Abstract methods are declared as follows:
abstract void fun();Copy the code
Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, the class is called abstract, and the abstract class must be preceded by the abstract keyword. You cannot create objects with abstract classes because they contain methods that have no concrete implementation.
In The Book Ideas for JAVA programming, an abstract class is defined as “a class that contains abstract methods.” However, it turns out that a class that contains no abstract methods is also an abstract class. That is, an abstract class does not have to contain abstract methods. Personally, I think this is a dead end question, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So keep the concept in mind for the moment, without trying to understand why.
[public] abstract class ClassName {
abstract void fun();
}
Copy the code
If you define an abstract class and don’t inherit it, you’re creating it for nothing because you can’t do anything with it. A parent class can be declared as an abstract method if one of its methods does not make any sense to implement in the parent class and must be implemented differently depending on the actual needs of the subclass.
The class containing abstract methods is called abstract class, but it does not mean that there can only be abstract methods in the abstract class, it and ordinary class, the same can have member variables and ordinary member methods. Note that there are three main differences between abstract classes and ordinary classes:
- The abstract method must be either public or protected (because if it is private, it cannot be inherited by subclasses, which cannot implement the method), and public by default.
- Abstract classes cannot be used to create objects;
- If a class inherits from an abstract class, the subclass must implement the parent class’s abstract methods. If a subclass does not implement the abstract methods of its parent class, it must also be defined as an abstract class.
In other respects, abstract classes are no different from ordinary classes.
Second, the interface
In software engineering, interface generally refers to a method or function called by others. From here, we can see the original intention of the Designers of the Java language, which is an abstraction of behavior. In Java, an interface has the following form:
[public] interface InterfaceName {
}
Copy the code
Interfaces can contain variables and methods. Note, however, that variables in the interface are implicitly specified as public static final variables (and only public static final variables). Methods are implicitly specified as public abstract methods and only public abstract methods (using other keywords such as private, protected, static, final, etc.), and all methods in the interface cannot be implemented. That is, the methods in the interface must all be abstract methods. An interface is an extremely abstract type, more “abstract” than an abstract class, and generally does not define variables in the interface.
To make a class conform to a specific set of interfaces, you use the implements keyword in the following format:
class ClassName implements Interface1,Interface2,[....]{
}
Copy the code
As you can see, a class is allowed to follow multiple specific interfaces. If a nonabstract class follows an interface, it must implement all the methods in that interface. Abstract classes that follow an interface may not implement abstract methods in that interface.
The difference between abstract classes and interfaces
1. Grammatical differences
- Abstract classes can provide implementation details for member methods, whereas only public abstract methods can exist in interfaces.
- Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type.
- Interfaces cannot have static code blocks and static methods, whereas abstract classes can have static code blocks and static methods.
- A class can inherit only one abstract class, while a class can implement multiple interfaces.
2. Differences at the design level
1) An abstract class is an abstraction of a thing, that is, an abstraction of a class, whereas an interface is an abstraction of behavior. An abstract class abstracts the class as a whole, including properties and behaviors, whereas an interface abstracts parts of the class (behavior). To take a simple example, airplanes and birds are different things, but they both have one thing in common, that is, they can fly. So when you design, you can design an Airplane like Airplane, you can design a Bird like Bird, but you can’t design flight as a class, so it’s just a behavioral property, it’s not an abstract description of a class of things. You can design flight as an interface, Fly, containing the method Fly (), which Airplane and Bird each implement according to their needs. For example, a fighter plane, a civil Airplane, or a Bird, you can inherit from Airplane. As you can see from this, inheritance is a “no” relationship, whereas interface implementation is a “no” relationship. If a class inherits an abstract class, the subclass must be the class of the abstract class, and the interface implementation is whether or not it has the relationship, such as whether birds can fly (or whether they have the characteristics of flight). If birds can fly, the interface can be implemented, and if they cannot fly, the interface will not be implemented.
2) The design level is different. Abstract class, as the parent of many subclasses, is a kind of template design. Interface is a code of conduct, it is a radiation design. What is template design? The simplest example is that everyone has used the template in PPT. If template A is used to design PPT B and PPT C, the common part of PPT B and PPT C is template A. If the common part of PPT B and PPT C needs to be changed, only template A needs to be changed, and there is no need to change PPT B and PPT C again. In radiant design, for example, an elevator is equipped with some kind of alarm, once the alarm has to be updated, it has to be updated. That is, for abstract classes, if you need to add new methods, you can directly add concrete implementation in the abstract class, the subclass can not change; This is not the case with interfaces. If an interface changes, all classes that implement that interface must change accordingly.
A door has two actions, open() and close(). In this case, we can define this abstract concept through abstract classes and interfaces:
abstract class Door {
public abstract void open();
public abstract void close();
}
Copy the code
Or:
interface Door {
public abstract void open();
public abstract void close();
}
Copy the code
But now if we need the door with alarm() function, then how to achieve? Here are two ideas:
1) Put these three functions in the abstract class, but so that all subclasses from this abstract class have alarm function, but some doors do not necessarily have alarm function;
2) Put all three functions in the interface. Classes that need alarm functions need to implement open() and close() in the interface. Maybe the class does not have open() and close() at all, such as fire alarm.
It can be seen from here that Door’s open(), close() and alarm() fundamentally belong to behaviors in two different categories. Open () and close() belong to the inherent behavior characteristics of the Door, while alarm() is an extended additional behavior. Therefore, the best solution is to design the alarm as a separate interface containing alarm() behavior, and the Door as a separate abstract class containing both open and close behavior. Then design an Alarm Door to inherit Door class and realize Alarm interface.
interface Alram {
void alarm();
}
abstract class Door {
void open();
void close();
}
class AlarmDoor extends Door implements Alarm {
void oepn() {/ /... } voidclose() {/ /... } voidalarm() {/ /... }}Copy the code
To summarize the differences between abstract classes and interfaces, use a table form:
parameter | An abstract class | interface |
Default method implementation | It can have a default method implementation | Interfaces are completely abstract. There is no implementation of the method at all |
implementation | Use a subclassextendsKeyword to inherit an abstract class. If a subclass is not abstract, it needs to provide implementations of all methods declared in the abstract class. | Subclasses use keywordsimplementsTo implement the interface. It needs to provide implementations of all declared methods in the interface |
The constructor | Abstract classes can have constructors | Interfaces cannot have constructors |
Differences from normal Java classes | It’s no different from regular Java classes except that you can’t instantiate an abstract class | Interfaces are of a completely different type |
Access modifier | Abstract methods can havepublic,protectedanddefaultThese modifiers | The default modifier for interface methods ispublic. You may not use other modifiers. |
The main method | An abstract method can have a main method and we can run it | The interface has no main method, so we can’t run it. |
Multiple inheritance | Abstract methods can inherit a class and implement multiple interfaces | An interface can only inherit from one or more other interfaces |
speed | It is faster than the interface speed | The interface is a bit slow because it takes time to find the methods implemented in the class. |
Add a new method | If you add a new method to an abstract class, you can provide it with a default implementation. So you don’t need to change your current code. | If you add methods to an interface, you must change the class that implements the interface. |