For object-oriented programming, abstraction is one of its major features. In Java, OOP abstraction can be reflected in two forms: interfaces and abstract classes. There are too many similarities and too many differences between the two. Many people think that they can be used interchangeably when they are beginners, but in fact they are not. Today we will learn about interfaces and abstract classes in Java. Here is the directory outline of this article:
1. Abstract Class
2. Interface
3. The difference between abstract classes and interfaces
1. Abstract Class
Before understanding abstract classes, let’s first understand abstract methods. An abstract method is a special method: it only has declarations, but no concrete implementation. The declaration format of an abstract method is:
abstract void fun();
Abstract methods must be modified with the abstract keyword. If a class contains abstract methods, this class is called an abstract class. The abstract class must be modified with the abstract keyword before the class. Because abstract classes contain methods without concrete implementations, objects cannot be created using abstract classes.
One issue should be noted below: In the book "JAVA Programming Thought", abstract classes are defined as "classes containing abstract methods", but later it was found that if a class does not contain abstract methods and is just modified with abstract, it is also an abstract class. In other words, abstract classes do not necessarily have to contain abstract methods. I personally think this is a stubborn question, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So remember this concept for the time being, and there is no need to dig into why.
[public] abstract class ClassName { abstract void fun();}From this we can see that abstract classes exist for inheritance. If you define an abstract class but do not inherit it, it is equivalent to creating this abstract class in vain, because you cannot use it to do anything. For a parent class, if one of its methods is implemented in the parent class has no meaning and must be implemented differently according to the actual needs of the subclass, then this method can be declared as an abstract method, and this class becomes an abstract class.
Classes containing abstract methods are called abstract classes, but they do not mean that abstract methods can only be found in abstract classes. Like ordinary classes, they can also have member variables and ordinary member methods. Note that there are three main differences between abstract classes and ordinary classes:
1) The abstract method must be public or protected (because if it is private, it cannot be inherited by the subclass, and the subclass cannot implement the method). By default, it is public.
2) Abstract classes cannot be used to create objects;
3) If a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. If the subclass does not implement the abstract method of the parent class, the subclass must also be defined as abstract class.
In other aspects, there is no difference between abstract classes and ordinary classes.
2. Interface
Interface, called interface in English, in software engineering, interfaces generally refer to methods or functions for others to call. From this, we can understand the original intention of Java language designers, which is an abstraction of behavior. In Java, the form of an interface is as follows:
[public] interface InterfaceName {}
Interfaces can contain variables and methods. However, it should be noted that variables in the interface will be implicitly specified as public static final variables (and can only be public static final variables, and modifying private will report a compilation error), while methods will be implicitly specified as public abstract method and can only be public abstract method (using other keywords, such as private, protected, static, final, etc. will report a compilation error), and all methods in the interface cannot have specific implementations, that is, methods in the interface must be abstract methods. From here, we can vaguely see the difference between an interface and an abstract class. An interface is an extremely abstract type. It is more "abstract" than an abstract class and generally does not define variables in an interface.
To make a class follow a specific group of interfaces, you need to use the implements keyword, the specific format is as follows:
class ClassName implements Interface1,Interface2,[....]{ }
It can be seen that a class is allowed to follow multiple specific interfaces. If a non-abstract class follows an interface, all methods in that interface must be implemented. For abstract classes that follow an interface, abstract methods in that interface may not be implemented.
3. The difference between abstract classes and interfaces
1. Differences at the grammatical level
1) Abstract classes can provide implementation details of member methods, while only public abstract methods can exist in the interface;
2) The member variables in abstract classes can be of various types, while the member variables in the interface can only be of public static final type;
3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
4) A class can only inherit one abstract class, while a class can implement multiple interfaces.
2. Differences at the design level
1) Abstract classes are abstractions of things, that is, abstractions of classes, while interfaces are abstractions of behavior. An abstract class abstracts the entire class, including properties and behaviors, but an interface abstracts the class part (behavior). To give a simple example, airplanes and birds are different things, but they all have one thing in common, that is, they all fly. Then when designing, the aircraft can be designed as an Airplane and a Bird-like character, but the flight characteristic cannot be designed as a class. Therefore, it is just a behavioral characteristic, not an abstract description of a type of thing. At this time, flight can be designed as an interface Fly, including method fly( ), and then Airplane and Bird implement the Fly interface according to their own needs. Then as for different types of aircraft, such as fighter jets, civil aircraft, etc., it can directly inherit the Airplane. It is also similar to birds. Different types of birds can directly inherit the Bird class. From here, we can see that inheritance is a "yes or not" relationship, while interface implementation is a "yes or not" relationship. If a class inherits an abstract class, the subclass must be a type of abstract class, and the interface implementation is a relationship that does not have, such as whether the bird can fly (or whether it has the characteristics of flying). If it can fly, it can realize this interface. If it cannot fly, it will not realize this interface.
2) Different design levels, abstract class is a template design, as the parent class of many subclasses. And the interface is a code of behavior, it is a radiation design. What is template design? The simplest example is that everyone has used the templates in ppt. If you use template A to design ppt B and ppt C, the common part of ppt B and ppt C is template A. If their public part needs to be changed, you only need to change template A, and there is no need to re-change ppt B and ppt C. Radiant design, such as an elevator equipped with some kind of alarm, once the alarm is to be updated, all must be updated. That is to say, for abstract classes, if you need to add new methods, you can directly add specific implementations to the abstract class, and subclasses can not be changed; but for interfaces, it is not possible. If the interface is changed, all classes that implement this interface must be changed accordingly.
Let’s see the most widely circulated example on the Internet: examples of doors and alarms: Doors have two actions: open( ) and close( ) . At this time, we can define this abstract concept through abstract classes and interfaces:
abstract class Door { public abstract void open(); public abstract void close();}or:
interface Door { public abstract void open(); public abstract void close();}But now if we need the door to have the function of alarm ( ) then how to implement it? The following two ideas are provided:
1) Put all three functions in the abstract class, but in this way, all subclasses inherited from this abstract class have alarm functions, but some doors do not necessarily have alarm functions;
2) Put all these three functions in the interface. Classes that need to use alarm functions need to implement open( ) and close( ) in this interface. Perhaps this class does not have the two functions of open( ) and close( ) at all, such as fire alarms.
From here, we can see that Door's open(), close() and alarm() simply belong to two behaviors in different categories. Open() and close() belong to the inherent behavior characteristics of the gate itself, while alarm() belongs to extended additional behaviors. Therefore, the best solution is to design the alarm as an interface, including alarm() behavior, and Door as a separate abstract class, including open and close behaviors. Another alarm gate is designed to inherit the Door class and implement the Alarm interface.
interface Alram { void alarm();} abstract class Door { void open(); void close();} class AlarmDoor extends Door implements Alarm { void oepn() { //.. } void close() { //.. } void alarm() { //... }}The above is a compilation of java interfaces and abstract classes. I hope it can help friends who learn java programming.