Home > Java > javaTutorial > body text

The difference between abstract classes and interfaces in Java at the syntax and design level

php是最好的语言
Release: 2018-07-27 10:04:09
Original
1870 people have browsed it

This article explains the difference between abstract classes and interfaces in Java: 1. Differences at the syntax level; 2. Differences at the design level.

1. Differences at the syntactic level

 1) Abstract classes can provide implementation details of member methods, while only public abstract methods can exist in interfaces;

 2) Abstraction Member variables in a class can be of various types, while member variables in an interface can only be of public static final type;

 3) Interfaces cannot contain static code blocks and static methods, while abstract classes can There are static code blocks and static methods;

 4) A class can only inherit one abstract class, but 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, while an interface is an abstraction of behavior. An abstract class abstracts the entire class, including attributes and behaviors, but an interface abstracts a part of the class (behavior). To give a simple example, airplanes and birds are different types of things, but they all have one thing in common, that is, they can fly. So when designing, you can design an airplane as an Airplane-like, and a bird as a Bird-like, but you cannot design the flying feature as a class, so it is only a behavioral feature, not an abstract description of a class of things. . At this time, flight can be designed as an interface Fly, including the 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., they can directly inherit Airplane. The same is true for birds. Different types of birds can directly inherit the Bird class. It can be seen from here that inheritance is a "is it" relationship, while interface implementation is a "is it is" relationship. If a class inherits an abstract class, the subclass must be of the type of abstract class, and the interface implementation is related to whether it exists or not, such as whether a bird can fly (or whether it has the characteristic of flying), whether it can fly Then you can implement this interface. If you can't fly, you can't implement this interface.

 2) The design level is different. The abstract class is the parent class of many subclasses, and it is a template design. The interface is a behavioral specification and it is a radial design. What is template 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 parts need to be changed, they only need to be modified. Template A is enough, there is no need to re-modify ppt B and ppt C. In the radial design, for example, an elevator is equipped with some kind of alarm. Once the alarms are updated, all the alarms must be updated. That is to say, for abstract classes, if you need to add new methods, you can add specific implementations directly to the abstract class, and the subclasses do not need to be changed; but for interfaces, this is not possible. If the interface is changed, all implementations of this interface will be affected. Classes must be modified accordingly.

Let’s look at one of the most widely circulated examples on the Internet: the door and alarm example: the door has two actions: open() and close(). At this time, we can define this abstraction through abstract classes and interfaces. Concept:

abstract class Door {

public abstract void open();
public abstract void close();
Copy after login
Copy after login

}

Or:

interface Door {

public abstract void open();
public abstract void close();
Copy after login
Copy after login

}

 But now if we need the door to have the alarm() function, how to implement it? Two ideas are provided below:

1) Put these three functions in the abstract class, but in this way all subclasses inherited from this abstract class have the alarm function, but some doors do not It does not necessarily have the alarm function;

 2) Put these three functions in the interface. The class that needs to use the alarm function needs to implement the open() and close() in this interface. Maybe this class There are no open() and close() functions at all, such as fire alarms.

It can be seen from here that Door’s open(), close() and alarm() basically belong to two different categories of behavior. open() and close() belong to the inherent behavioral characteristics of the door itself. , and alarm() is an extended additional behavior. Therefore, the best solution is to design the alarm as an interface separately, including the alarm() behavior, and design the Door as a separate abstract class, including open and close behaviors. Then design an alarm door that inherits the Door class and implements the Alarm interface.

interface Alram {

void alarm();
Copy after login

}

abstract class Door {

void open();
void close();
Copy after login

}

class AlarmDoor extends Door implements Alarm {

void oepn() {
  //....
}
void close() {
  //....
}
void alarm() {
  //....
}
Copy after login

}

Related articles:

The difference between interface and abstract class, the difference between interface abstract class

in java Interfaces and abstract classes

Related videos:

The difference between interface and abstract class-the latest Java complete video tutorial

The above is the detailed content of The difference between abstract classes and interfaces in Java at the syntax and design level. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template