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. At this time, 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 {
 
}

接口中可以含有 变量和方法。但是要注意,接口中的变量会被隐式地指定为public static final变量(并且只能是public static final变量,用private修饰会报编译错误),而方法会被隐式地指定为public abstract方法且只能是public abstract方法(用其他关键字,比如private、protected、static、 final等修饰会报编译错误),并且接口中所有的方法不能有具体的实现,也就是说,接口中的方法必须都是抽象方法。从这里可以隐约看出接口和抽象类的区别,接口是一种极度抽象的类型,它比抽象类更加“抽象”,并且一般情况下不在接口中定义变量。

要让一个类遵循某组特地的接口需要使用implements关键字,具体格式如下:

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

三.抽象类和接口的区别

1.语法层面上的区别

1)抽象类可以提供成员方法的实现细节,而接口中只能存在public abstract 方法;

2)抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是public static final类型的;

3)接口中不能含有静态代码块以及静态方法,而抽象类可以有静态代码块和静态方法;

4)一个类只能继承一个抽象类,而一个类却可以实现多个接口。

2.设计层面上的区别

1)抽象类是对一种事物的抽象,即对类抽象,而接口是对行为的抽象。抽象类是对整个类整体进行抽象,包括属性、行为,但是接口却是对类局部(行为)进行抽象。举个简单的例子,飞机和鸟是不同类的事物,但是它们都有一个共性,就是都会飞。那么在设计的时候,可以将飞机设计为一个类Airplane,将鸟设计为一个类Bird,但是不能将 飞行 这个特性也设计为类,因此它只是一个行为特性,并不是对一类事物的抽象描述。此时可以将 飞行 设计为一个接口Fly,包含方法fly( ),然后Airplane和Bird分别根据自己的需要实现Fly这个接口。然后至于有不同种类的飞机,比如战斗机、民用飞机等直接继承Airplane即可,对于鸟也是类似的,不同种类的鸟直接继承Bird类即可。从这里可以看出,继承是一个 “是不是”的关系,而 接口 实现则是 “有没有”的关系。如果一个类继承了某个抽象类,则子类必定是抽象类的种类,而接口实现则是有没有、具备不具备的关系,比如鸟是否能飞(或者是否具备飞行这个特点),能飞行则可以实现这个接口,不能飞行就不实现这个接口。

2)设计层面不同,抽象类作为很多子类的父类,它是一种模板式设计。而接口是一种行为规范,它是一种辐射式设计。什么是模板式设计?最简单例子,大家都用过ppt里面的模板,如果用模板A设计了ppt B和ppt C,ppt B和ppt C公共的部分就是模板A了,如果它们的公共部分需要改动,则只需要改动模板A就可以了,不需要重新对ppt B和ppt C进行改动。而辐射式设计,比如某个电梯都装了某种报警器,一旦要更新报警器,就必须全部更新。也就是说对于抽象类,如果需要添加新的方法,可以直接在抽象类中添加具体的实现,子类可以不进行变更;而对于接口则不行,如果接口进行了变更,则所有实现这个接口的类都必须进行相应的改动。

下面看一个网上流传最广泛的例子:门和警报的例子:门都有open( )和close( )两个动作,此时我们可以定义通过抽象类和接口来定义这个抽象概念:

abstract class Door {
    public abstract void open();
    public abstract void close();
}

或者:

interface Door {
    public abstract void open();
    public abstract void close();
}

但是现在如果我们需要门具有报警alarm( )的功能,那么该如何实现?下面提供两种思路:

1)将这三个功能都放在抽象类里面,但是这样一来所有继承于这个抽象类的子类都具备了报警功能,但是有的门并不一定具备报警功能;

2)将这三个功能都放在接口里面,需要用到报警功能的类就需要实现这个接口中的open( )和close( ),也许这个类根本就不具备open( )和close( )这两个功能,比如火灾报警器。

从这里可以看出, Door的open() 、close()和alarm()根本就属于两个不同范畴内的行为,open()和close()属于门本身固有的行为特性,而alarm()属于延伸的附加行为。因此最好的解决办法是单独将报警设计为一个接口,包含alarm()行为,Door设计为单独的一个抽象类,包含open和close两种行为。再设计一个报警门继承Door类和实现Alarm接口。

interface Alram {
    void alarm();
}
 
abstract class Door {
    void open();
    void close();
}
 
class AlarmDoor extends Door implements Alarm {
    void oepn() {
      //....
    }
    void close() {
      //....
    }
    void alarm() {
      //....
    }
}

参考资料:

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=325535965&siteId=291194637