For object-oriented programming, abstraction is one of its major features. In Java, the abstraction of OOP can be embodied in two forms:interfaces and abstract classes.There are many similarities between the two, and also many differences. Many beginners think they can be used interchangeably, but in fact that is not the case. Today, let's learn about interfaces and abstract classes in Java together.
1. Abstract Classes
Before understanding abstract classes, let's first understand abstract methods. An abstract method is a special method: it has only a declaration, 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 an abstract method, it is called an abstract class, and the abstract class must be modified with the abstract keyword before the class. Because an abstract class contains methods without concrete implementation, you cannot create objects from an abstract class.
Note the following issue: In the book "Thinking in Java", an abstract class is defined as "a class containing abstract methods", but later it was found that if a class does not contain abstract methods and is only modified with abstract, it is also an abstract class. That is to say, an abstract class does not necessarily have to contain abstract methods. Personally, I think this is a pedantic question, because if an abstract class does not contain any abstract methods, why design it as an abstract class? So just remember this concept for now, and there is no need to delve into why.
[public] abstract class ClassName {
abstract void fun();
}
It can be seen from this that abstract classes exist for inheritance. If you define an abstract class but do not inherit it, then you have created the abstract class in vain, because you cannot use it to do anything. For a parent class, if one of its methods has no meaning when implemented in the parent class and must be implemented differently according to the actual needs of subclasses, then this method can be declared as an abstract method, and at this point the class also becomes an abstract class.
A class containing abstract methods is called an abstract class, but that does not mean an abstract class can only have abstract methods. Like an ordinary class, it can also have member variables and ordinary member methods. Note that there are three main differences between abstract classes and ordinary classes:
- 1) Abstract methods must be public or protected (because if they are private, they cannot be inherited by subclasses, and subclasses will not be able to implement them). By default, they are 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 methods of the parent class. If the subclass does not implement the abstract methods of the parent class, the subclass must also be defined as an abstract class.
- In other respects, abstract classes are no different from ordinary classes.
2. Interfaces
An interface, called "interface" in English, in software engineering generally refers to methods or functions that are provided for others to call. From this, we can appreciate the original intention of Java language designers: it is an abstraction of behavior. In Java, the form for defining an interface is as follows:
[public] interface InterfaceName {
}
Interfaces can contain variables and methods. However, note that variables in an interface are implicitly specified as public static final variables (and can only be public static final variables; using private will cause a compilation error), while methods are implicitly specified as public abstract methods and can only be public abstract methods (using other keywords such as private, protected, static, final, etc. will cause a compilation error). Moreover, none of the methods in an interface can have a concrete implementation; that is, all methods in an interface must be abstract methods. From this, we can vaguely see the difference between interfaces and abstract classes. An interface is an extremely abstract type; it is more "abstract" than an abstract class, and variables are generally not defined in an interface.
To make a class implement a specific set 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 implement multiple specific interfaces. If a non-abstract class implements an interface, it must implement all the methods in that interface. For an abstract class that implements an interface, it does not have to implement the abstract methods in that interface.
Differences between Abstract Classes and Interfaces
1. Syntactic Differences
- 1) Abstract classes can provide implementation details of member methods, while interfaces can only have public abstract methods;
- 2) Member variables in abstract classes can be of various types, while member variables in interfaces can only be of type public static final;
- 3) Interfaces cannot contain static code blocks or static methods, while abstract classes can have static code blocks and static methods;
- 4) A class can only inherit one abstract class, but a class can implement multiple interfaces.
2. Design-Level Differences
1) An abstract class is an abstraction of a kind of thing, that is, an abstraction of a class, while an interface is an abstraction of behavior. An abstract class abstracts the entire class as a whole, including attributes and behaviors, but an interface abstracts only a part of a class (behavior). For a simple example, airplanes and birds are different kinds of things, but they share a common characteristic: they can both fly. When designing, you can design the airplane as a class Airplane and the bird as a class Bird, but you cannot treatflightas a class either, because it is only a behavioral feature, not an abstract description of a kind of thing. At this point, you can design flight as an interface Fly, containing the method fly(), and then Airplane and Bird implement the Fly interface according to their own needs. As for different kinds of airplanes, such as fighter jets and civil aircraft, they can directly inherit Airplane. Similarly for birds, different kinds of birds can directly inherit the Bird class. From this, we can see that inheritance is an "is a" relationship, while interface implementation is a "has a" relationship. If a class inherits an abstract class, then the subclass must be a kind of the abstract class, while interface implementation is about whether it has or possesses a certain capability. For example, whether a bird can fly (or whether it has the characteristic of flight); if it can fly, it can implement this interface, if it cannot fly, it does not implement this interface.
2) The design levels are different. An abstract class, as the parent class of many subclasses, is a template-based design. An interface is a behavioral specification, a radial design. What is template-based design? The simplest example is that everyone has used templates in PPT. If ppt B and ppt C are designed using template A, the common part of ppt B and ppt C is template A. If their common part needs to be changed, you only need to change template A, and there is no need to modify ppt B and ppt C again. Radial design, for example, if a certain elevator is equipped with a certain alarm, once the alarm needs to be updated, all of them must be updated. That is to say, for an abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, and subclasses do not need to change; but this is not the case for an interface. If the interface changes, all classes that implement the interface must be changed accordingly.
Below is the most widely circulated example on the Internet: the example of doors and alarms: all doors haveopen()andclose()two actions. At this point, we can define this abstract concept through abstract classes and interfaces:
Or:
But now if we need the door to have an alarm function, how should we implement it? Below are two approaches:
1) Put all three functions in the abstract class, but in this way all subclasses inheriting from this abstract class will have the alarm function, but some doors may not necessarily have the alarm function;
2) Put all three functions in the interface. A class that needs the alarm function would then have to implement the open() and close() in this interface, but perhaps this class does not have the open() and close() functions at all, such as a fire alarm.
It can be seen from this that Door's open(), close(), and alarm() actually belong to behaviors in two different categories. open() and close() are inherent behavioral characteristics of the door itself, while alarm() is an extended additional behavior. Therefore, the best solution is to design the alarm separately as an interface containing the alarm() behavior, and design Door as a separate abstract class containing the two behaviors open and close. Then design an AlarmDoor class that inherits the Door class and implements the Alarm interface.
Original address: http://www.cnblogs.com/dolphin0520/p/3811437.html