Deep understanding of Java interfaces and abstract classes

Reprinted from a  deep understanding of Java's interfaces and abstract classes

Abstraction is one of the hallmarks of object-oriented programming. In Java, OOP abstraction can be embodied in two forms: interfaces and abstract classes. The two have too many similarities and too many differences. Many people think they can be used interchangeably when they are first learning, but they are not. Today we will learn about interfaces and abstract classes in Java. Here is the table of contents outline for this article:

1. Abstract class

2. Interface

3. The difference between abstract class and interface

If there are any inaccuracies, please forgive me and welcome criticism and corrections, I am very grateful.

1. Abstract class

Before learning about abstract classes, let’s first understand abstract methods. An abstract method is a special kind of 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 decorated with the abstract keyword. If a class contains abstract methods, the class is called abstract class, and the abstract class must be modified with the abstract keyword before the class. An abstract class cannot be used to create objects because an abstract class contains methods that have no concrete implementation.

  The following problem should be paid attention to: in the book " Java Programming Thought ", an abstract class is defined as "a class containing abstract methods", but later it is found that if a class does not contain abstract methods, it is also an abstract class if it is only modified with abstract. That is to say, an abstract class does not necessarily have to contain abstract methods. Personally, I think this is a serious problem, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So keep this concept in mind for now, without getting 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 it is equivalent to creating this abstract class in vain, because you cannot use it to do anything. For a parent class, if a certain method of it is implemented in the parent class without any 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 the class is also It becomes the abstract class.

A class that contains abstract methods is called an abstract class, but it does not mean that an abstract class can only have abstract methods. Like ordinary classes, it 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 subclasses, and subclasses cannot implement the method), and it defaults to public by default.

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 superclass. If the subclass does not implement the abstract method of the superclass, the subclass must also be defined as an abstract class.

In other respects, abstract classes are no different from ordinary classes.

2. Interface

Interface, called interface in English, in software engineering, an interface generally refers to a method or function for others to call. From here, we can appreciate the original intention of the Java language designers, which is an abstraction of behavior. In Java, an interface is defined as follows:

[public] interface InterfaceName {
 
}

An interface can contain variables and methods. However, it should be noted that the variables in the interface will be implicitly designated as public static final variables (and can only be public static final variables, a compilation error will be reported with private modification), and methods will be implicitly designated as public abstract methods And it can only be a public abstract method (modified with other keywords, such as private, protected, static, final, etc., a compilation error will be reported), and all methods in the interface cannot have specific implementations, that is, the methods in the interface must be are abstract methods. From here, you can vaguely see the difference between an interface and an abstract class. An interface is an extremely abstract type, which is more "abstract" than an abstract class, and generally does not define variables in an interface.

To make a class conform to a specific set of interfaces , use the implements keyword, in the following format:

class ClassName implements Interface1,Interface2,[....]{
}
可以看出,允许一个类遵循多个特定的接口。如果一个非抽象类遵循了某个接口,就必须实现该接口中的所有方法。对于遵循某个接口的抽象类,可以不实现该接口中的抽象方法。

3. The difference between abstract class and interface

1. Differences at the grammatical level

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

2) Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type;

3) An 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, 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 whole 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 things, but they all have one thing in common, that is, they both fly. Then when designing, you can design the aircraft as a class Airplane, and design the bird as a class Bird, but you cannot design the feature of flight as a class, so it is just a behavioral feature, not an abstract description of a class of things . At this time, the flight can be designed as an interface Fly, including the method fly( ), and then Airplane and Bird respectively implement the Fly interface according to their own needs. Then there are different types of aircraft, such as fighter jets, civil aircraft, etc., can directly inherit Airplane, and the same is true for birds, different types of birds can directly inherit the Bird class. It can be seen from this that inheritance is a "is or not" relationship, while interface implementation is a "is there or not" relationship. If a class inherits an abstract class, the subclass must be the type of abstract class, and the interface implementation is the relationship between whether it has or not, such as whether the bird can fly (or whether it has the feature of flying), can fly You can implement this interface, but if you can't fly, you can't implement this interface.

2) The design level is different. As the parent class of many subclasses, the abstract class is a template design. The interface is a code of behavior, it is a radial design. What is template design? The simplest example, everyone has used the templates 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 their common parts need to be changed, only need to be changed Template A is fine, 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 to be updated, they 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 the subclass can not change it; but not for the interface, if the interface is changed, all the implementations of this interface Classes must be modified accordingly.

Let's look at one of the most widely circulated examples on the Internet: the example of a door and an alarm: the door has 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( ), how to do it? Here are two ideas:

1) Put these three functions in the abstract class, but in this way, all subclasses that inherit from this abstract class have the alarm function, but some doors do 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 does not have the open( ) and close( ) functions at all. Two functions, such as a fire alarm.

It can be seen from this that Door's open(), close() and alarm() belong to behaviors in two different categories at all. open() and close() belong to the inherent behavioral characteristics of the door itself, while alarm() belongs to Extended additional behavior. Therefore, the best solution is to design the alarm as an interface, including the behavior of alarm(), and design the Door as a separate abstract class, including two behaviors of open and close. Design another 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() {
      //....
    }
}

References:

http://blog.csdn.net/chenssy/article/details/12858267

http://dev.yesky.com/436/7581936.shtml

http://blog.csdn.net/xw13106209/article/details/6923556

http://android.blog.51cto.com/268543/385282/

http://peiquan.blog.51cto.com/7518552/1271610


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325535877&siteId=291194637