For object-oriented programming, abstraction is one of its major features. 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 that they can be used interchangeably when they first learn, but this is not the case in reality. Today we will learn about interfaces and abstract classes in Java. The following is the table of contents outline of this article:
1. Abstract class
2. Interface
3. The difference between abstract class and interface
If If there are any inaccuracies, please forgive me and welcome criticisms and corrections. Thank you very much.
Before understanding abstract classes, let’s first understand abstract methods. An abstract method is a special kind of method: it only has a declaration, but no concrete implementation. The declaration format of abstract methods is:
<code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fun</span><span class="hljs-params">()</span></span>;</code>
Abstract methods must be modified with the abstract keyword. If a class contains abstract methods, the class is called an abstract class. Abstract classes must be modified with the abstract keyword before the class. Because abstract classes contain methods that have no concrete implementation, objects cannot be created using abstract classes.
The following should be noted: In the book "Java Programming Thoughts", an abstract class is defined as a "class containing abstract methods", but later it was discovered that if a class It does not contain abstract methods. If it is just modified with abstract, it is also an abstract class. In other words, abstract classes do not necessarily have to contain abstract methods. Personally, I think this is a tricky question, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So let’s remember this concept for now without going into why.
<ol class="hljs-ln list-paddingleft-2"><li><p class="hljs-ln-line">[<span class="hljs-keyword">public</span>] <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassName</span> </span>{</p></li><li><p class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fun</span><span class="hljs-params">()</span></span>;</p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span></p></li></ol>
It can be seen from here that abstract classes exist for inheritance. If you define an abstract class but do not inherit it, then you have created this abstract class in vain because you cannot use it. to do anything. For a parent class, if one of its methods does not make any sense when implemented in the parent class 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 also It becomes an abstract class.
A class that contains abstract methods is called an abstract class, but it does not mean that abstract classes can only have abstract methods. Like ordinary classes, they 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 cannot implement the method). In provincial cases, the default is 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 implement the abstract method of the parent class, the subclass must also be defined as an abstract class.
In other aspects, there is no difference between abstract classes and ordinary classes.
Interface is called interface in English. In software engineering, interface generally refers to methods or functions that are called by others. From here, we can realize the original intention of the Java language designer, which is the abstraction of behavior. In Java, the form of defining an interface is as follows:
<ol class="hljs-ln list-paddingleft-2"><li><p class="hljs-ln-numbers">[public] interface InterfaceName {</p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span></p></li></ol>
The interface can contain variables and methods. But please note that the variables in the interface will be implicitly designated as public static final variables (and they can only be public static final variables. Modifying them with private will report a compilation error), while the methods will be implicitly designated as public abstract methods. And it can only be a public abstract method (modification with other keywords, such as private, protected, static, final, etc. will report a compilation error), and all methods in the interface cannot have specific implementations, that is to say, the methods in the interface must They are all abstract methods. From here you can vaguely see the difference between interfaces and abstract classes. Interfaces are an extremely abstract type. They are more "abstract" than abstract classes, and variables are generally not defined in interfaces.
To make a class follow a specific set of interfaces, you need to use the implements keyword. The specific format is as follows:
<ol class="hljs-ln list-paddingleft-2"><li><p class="hljs-ln-numbers">class ClassName implements Interface1,Interface2,[....]{</p></li><li><p class="hljs-ln-line">}</p></li></ol>
It can be seen that a class is allowed to follow multiple specific interfaces. If a non-abstract class conforms to an interface, it must implement all methods in the interface. For an abstract class that follows an interface, it is not necessary to implement the abstract methods in the interface.
1. Differences at the syntax level
1) Abstract classes can provide implementation details of member methods, while interfaces can only There is a public abstract method;
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) Interface It 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)抽象类是对一种事物的抽象,即对类抽象,而接口是对行为的抽象。抽象类是对整个类整体进行抽象,包括属性、行为,但是接口却是对类局部(行为)进行抽象。举个简单的例子,飞机和鸟是不同类的事物,但是它们都有一个共性,就是都会飞。那么在设计的时候,可以将飞机设计为一个类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( )两个动作,此时我们可以定义通过抽象类和接口来定义这个抽象概念:
<ol class="hljs-ln list-paddingleft-2"><li><p class="hljs-ln-numbers">abstract class Door {</p></li><li><p class="hljs-ln-numbers">public abstract void open();<br/></p><p class="hljs-ln-line hljs-ln-n"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span></span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">;</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span></p></li></ol>
或者:
<ol class="hljs-ln list-paddingleft-2"><li><p class="hljs-ln-numbers">interface Door {</p></li><li><p class="hljs-ln-numbers">public abstract void open();</p></li><li><p class="hljs-ln-numbers">public abstract void close();<br/></p><p class="hljs-ln-line hljs-ln-n"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span></p></li></ol>
但是现在如果我们需要门具有报警alarm( )的功能,那么该如何实现?下面提供两种思路:
1)将这三个功能都放在抽象类里面,但是这样一来所有继承于这个抽象类的子类都具备了报警功能,但是有的门并不一定具备报警功能;
2)将这三个功能都放在接口里面,需要用到报警功能的类就需要实现这个接口中的open( )和close( ),也许这个类根本就不具备open( )和close( )这两个功能,比如火灾报警器。
从这里可以看出, Door的open() 、close()和alarm()根本就属于两个不同范畴内的行为,open()和close()属于门本身固有的行为特性,而alarm()属于延伸的附加行为。因此最好的解决办法是单独将报警设计为一个接口,包含alarm()行为,Door设计为单独的一个抽象类,包含open和close两种行为。再设计一个报警门继承Door类和实现Alarm接口。
<code class="language-java hljs"></code><ol class="hljs-ln list-paddingleft-2"><li><p class="hljs-ln-numbers">interface Alram {</p></li><li><p class="hljs-ln-numbers"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">void</span> <span class="hljs-title">alarm</span><span class="hljs-params">()</span></span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">;</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}<br/></span></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><br/></span><span class="hljs-keyword" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">abstract</span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"> </span><span class="hljs-class" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">class</span> <span class="hljs-title">Door</span> </span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">{</span></p></li><li><p class="hljs-ln-numbers"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span></span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">;</span><br/></p></li><li><p class="hljs-ln-numbers"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span></span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">;</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span><br/></p><p class="hljs-ln-numbers"><span class="hljs-class" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">class</span> <span class="hljs-title">AlarmDoor</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Door</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Alarm</span> </span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">{</span><br/></p></li><li><p class="hljs-ln-numbers"><br/></p><p class="hljs-ln-line hljs-ln-n"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">void</span> <span class="hljs-title">oepn</span><span class="hljs-params">()</span> </span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">{</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">//....</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span><br/></p></li><li><p class="hljs-ln-numbers"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span> </span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">{</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">//....</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span><br/></p></li><li><p class="hljs-ln-numbers"><span class="hljs-function" style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;"><span class="hljs-keyword">void</span> <span class="hljs-title">alarm</span><span class="hljs-params">()</span> </span><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">{</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">//....</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span><br/></p></li><li><p class="hljs-ln-numbers"><span style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;">}</span></p></li></ol>
相关推荐:
The above is the detailed content of Java interfaces and abstract classes and the difference between the two. For more information, please follow other related articles on the PHP Chinese website!