Home >Java >javaTutorial >Detailed explanation of interfaces and abstract classes in Java

Detailed explanation of interfaces and abstract classes in Java

黄舟
黄舟Original
2017-09-19 09:50:511252browse

This article mainly introduces relevant information for in-depth understanding of Java interfaces and abstract classes. I hope that through this article you can understand how to use application interfaces and abstract classes. Friends in need can refer to it

Java's in-depth understanding of interfaces and abstract classes

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.

1. Abstract class

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 an abstract method is:

abstract void fun();

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 does not contain abstract methods, 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.

[public] abstract class ClassName {
  abstract void fun();
}

It can be seen from here that abstract classes exist for inheritance. If you define an abstract class but do not inherit it, then it is equivalent to You created this abstract class in vain because you can't do anything with it. 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.

2. Interface

Interface, called interface in English, in software engineering, 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:

[public] interface InterfaceName {

}

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, and private modification 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:

class ClassName implements Interface1,Interface2,[....]{
}

As you can see, it is allowed A class follows 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.

3. The difference between abstract classes and interfaces

1. Differences at the syntax level

1) Abstract classes can provide member methods Implementation details, and only public abstract methods can 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) Interfaces 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( )两个动作,此时我们可以定义通过抽象类和接口来定义这个抽象概念:

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();
}
/**
 * Java学习交流QQ群:589809992 我们一起学Java!
 */
class AlarmDoor extends Door implements Alarm {
  void oepn() {
   //....
  }
  void close() {
   //....
  }
  void alarm() {
   //....
  }
}

The above is the detailed content of Detailed explanation of interfaces and abstract classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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