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 too many similarities and there are so many different places. Many people think they can be used interchangeably when they are beginners, but they are not. Today we'll come together to learn the interfaces and abstract classes in Java. The following is a table of contents outline for this article:
I. Abstract class
Two. Interface
Three. The difference between abstract classes and interfaces
I. Abstract class
Before you learn about abstract classes, let's take a look at the abstract methods. An abstract method is a special method: it has only declarations, but no concrete implementations. The declaration format for an abstract method is:
abstract void Fun ();
Abstract methods must be decorated with the abstract keyword. If a class contains an abstract method, the class is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because an abstract class contains methods that do not have a specific implementation, you cannot create an object with an abstract class.
Here's a question: In the Java programming idea, you define an abstract class as "a class that contains abstract methods," but later you find that if a class does not contain abstract methods, it is also an abstract class if it is modified in abstract. This means that an abstract class does not necessarily have to contain abstract methods. Personally think this is a matter of concern, because if an abstract class does not contain any abstract methods, why also design as an abstract class? So remember this concept for a moment, without having to delve into why.
[public] abstract class ClassName {
abstract void fun ();
}
As you can see from here, an abstract class exists for inheritance, and if you define an abstract class and do not inherit it, it is tantamount to creating the abstract class in vain, because you cannot do anything with it. For a parent class, if one of its methods does not make sense in the parent class, it must be implemented differently depending on the actual requirements of the subclass, so that the method can be declared as an abstract method, at which point the class becomes an abstract class.
A class that contains abstract methods is called an abstract class, but does not imply that there can be only abstract methods in an abstract class, which, like ordinary classes, can also have member variables and ordinary member methods. Note that there are three main differences between an abstract class and an ordinary class:
1 The abstract method must be public or protected (because if private, you cannot inherit the quilt class, the subclass will not implement the method), by default, 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 have an abstract method that implements the parent class, the subclass must also be defined as an abstract class.
In other ways, there is no difference between an abstract class and an ordinary class.
Two. Interface
interface, English called interface, in software engineering, interfaces refer to methods or functions that are called by others. From here, we can understand the Java language Designer's original intention, it is the abstraction of behavior. In Java, you set an interface in the following form:
[Public] interface InterfaceName {}
The interface can contain variables and methods. Note, however, that the variables in the interface are implicitly specified as public static final variables (and only public static final variables, which are reported to compile errors with private adornments), and methods are implicitly specified as public The abstract method can only be public abstract (with other keywords, such as private, protected, static, final, etc.), and all the methods in the interface cannot have a specific implementation, that is to say, The methods in the interface must all be abstract methods. From here you can see the difference between the interface and the abstract class, the interface is an extremely abstract type, it is more "abstract" than the abstract class, and generally does not define variables in the interface.
To have a class follow a specific set of interfaces, you need to use the Implements keyword, which is as follows:
Class ClassName implements interface1,interface2,[...] { }
As you can see, a class is allowed to follow multiple specific interfaces. If a Non-abstract class follows an interface, all the methods in that interface must be implemented. Abstract methods in this interface are not implemented for abstract classes that follow an interface.
Three. The difference between abstract classes and interfaces
1. Differences on the grammatical level
1 abstract class can provide the implementation details of the member method, while the interface can only exist public abstract method;
2 The member variables in the abstract class can be of various types, while the member variables in the interface can only be public static final types;
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 inherit only one abstract class, while a class may implement multiple interfaces.
2. Design-level differences
1 abstract class is an abstraction of a thing, that is, a class abstraction, and an interface is an abstraction of the behavior. An abstract class is an abstraction of the whole class as a whole, including attributes, behavior, but an interface that abstracts the local (behavior) of the class. For a simple example, airplanes and birds are different kinds of things, but they all have one thing in common, they all fly. Then in the design, the aircraft can be designed as a class airplane, the birds are designed as a class bird, but can not fly this feature is also designed as a class, so it is only a behavioral characteristics, not the abstract description of a class of things. The flight can then be designed as an interface fly, containing the method fly (), and then airplane and bird implement fly this interface separately according to their own needs. Then as for different kinds of aircraft, such as fighter jets, civil aircraft, such as direct inheritance airplane can, for birds are similar, different kinds of birds directly inherit bird class can. As can be seen from here, inheritance is a "is not" relationship, and interface implementation is "there is no" relationship. If a class inherits an abstract class, then the subclass must be the kind of abstract class, and the interface implementation is there is no, have no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can implement this interface, can not fly do not implement this interface.
2 The design level is different, abstract class as many subclasses of the parent class, it is a template design. And the interface is a code of conduct, it is a kind of radiant design. What is a template design? The simplest example, we have used the template in ppt, if using template A to design ppt B and ppt c,ppt B and ppt C public part is the template A, if their public part needs to change, then only need to change template A on it, do not need to change PPT B and ppt c. And the radiant design, such as a certain elevator installed some kind of alarm, once you want to update the alarm, you must update all. That is to say, for an abstract class, if you need to add a new method, you can add a specific implementation directly to the abstract class, and the subclass can not make changes, but not for the interface, and if the interface is changed, all classes that implement this interface must be changed accordingly.
Here's one of the most popular examples of the Web: doors and Alarms: doors have open () and close () two actions, at which point 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 function of the gate to have the alarm alarm (), how do we implement it? Here are two ways to do this:
1 The three functions are placed in the abstract class, but so all the subclasses inherited from this abstract class have the alarm function, but some doors do not necessarily have the alarm function;
2 The three functions are placed in the interface, the need to use the alarm function of the class need to implement the interface Open () and close (), perhaps this class does not have the open () and close () these two functions, such as fire alarm.
It can be seen from here that the door open (), close () and alarm () are essentially two different categories of behavior, open () and close () are intrinsic to the door itself, and alarm () is an extension of the additional behavior. The best solution, therefore, is to design the alarm individually as an interface, containing the alarm () behavior, door designed as a separate abstract class that contains open and close two behaviors. Then design an alarm door 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 the Java interface and abstract class data collation, hoping to help learn Java programming friends.