1. Overview
1.概述
In this tutorial, we’ll discuss inheritance, one of the crucial concepts of Object-Oriented Programming. In Java, the two main keywords used for inheritance are extends and implements.
在本教程中,我们将讨论继承,这是面向对象编程的关键概念之一。在Java中,用于继承的两个主要关键词是extends和implements。
2. extends vs. implements
2.扩展与实施
Let’s discuss the differences between both the keywords.
让我们讨论一下这两个关键词之间的区别。
We use the extends keyword to inherit properties and methods from a class. The class that acts as a parent is called a base class, and the class that inherits from this base class is called a derived or a child class. Mainly, the extends keyword is used to extend the functionality of a parent class to the derived classes. Also, a base class can have many derived classes, but a derived class can have only one base class because Java doesn’t support multiple inheritance.
我们使用extends关键字来继承一个类的属性和方法。作为父类的类被称为基类,而继承自该基类的类被称为派生类或子类。主要来说,extends关键字被用来将父类的功能扩展到派生类中。另外,一个基类可以有很多派生类,但一个派生类只能有一个基类,因为Java不支持多重继承。
On the other hand, we use the implements keyword to implement an interface. An interface consists only of abstract methods. A class will implement the interface and define these abstract methods as per the required functionality. Unlike extends, any class can implement multiple interfaces.
另一方面,我们使用implements关键字来实现一个接口。一个接口仅由抽象方法组成。一个类将实现该接口,并根据所需的功能来定义这些抽象方法。与extends不同,任何类都可以实现多个接口。
Although both the keywords align with the concept of inheritance, the implements keyword is primarily associated with abstraction and used to define a contract, and extends is used to extend a class’s existing functionality.
尽管这两个关键字都与继承的概念一致,但implements关键字主要与抽象相关,用于定义契约,而extends则用于扩展类的现有功能。
3. Implementation
3.实施
Let’s jump to the implementation and have look at extends, implements, and multiple inheritance one by one, in detail.
让我们跳到实现部分,逐一详细了解extends、implements和多重继承。
3.1. extends
3.1. 扩展
Let’s start by creating a class called Media that has id, title, and artist. This class will act as a base class. VideoMedia and AudioMedia will extend the functionality of this class:
让我们先创建一个名为Media的类,它有id、title和artist。这个类将作为一个基类。VideoMedia和AudioMedia将扩展这个类的功能。
public class Media {
private int id;
private String title;
private String artist;
// standard getters and setters
}
Now, let’s create another class called VideoMedia that extends the class Media, inheriting its properties. Additionally, it has its own properties like resolution and aspectRatio:
现在,让我们创建另一个名为VideoMedia的类,它扩展了Media类,继承其属性。此外,它有自己的属性,如resolution和aspectRatio。
public class VideoMedia extends Media {
private String resolution;
private String aspectRatio;
// standard getters and setters
}
Similarly, the class AudioMedia also extends the class Media and will have its own additional properties like bitrate and frequency:
同样地,类AudioMedia也扩展类Media,并将有自己的额外属性,如bitrate和frequency。
public class AudioMedia extends Media {
private int bitrate;
private String frequency;
// standard getters and setters
@Override
public void printTitle() {
System.out.println("AudioMedia Title");
}
}
Let’s create objects for the base and derived classes to look at the inherited properties:
让我们为基类和派生类创建对象,看一下继承的属性。
Media media = new Media();
media.setId(001);
media.setTitle("Media1");
media.setArtist("Artist001");
AudioMedia audioMedia = new AudioMedia();
audioMedia.setId(101);
audioMedia.setTitle("Audio1");
audioMedia.setArtist("Artist101");
audioMedia.setBitrate(3500);
audioMedia.setFrequency("256kbps");
VideoMedia videoMedia = new VideoMedia();
videoMedia.setId(201);
videoMedia.setTitle("Video1");
videoMedia.setArtist("Artist201");
videoMedia.setResolution("1024x768");
videoMedia.setAspectRatio("16:9");
System.out.println(media);
System.out.println(audioMedia);
System.out.println(videoMedia);
All three classes print the associated properties:
这三个类都会打印相关的属性。
Media{id=1, title='Media1', artist='Artist001'}
AudioMedia{id=101, title='Audio1', artist='Artist101', bitrate=3500, frequency='256kbps'}
VideoMedia{id=201, title='Video1', artist='Artist201'resolution='1024x768', aspectRatio='16:9'}
3.2. implements
3.2 工具
In order to understand abstraction and interfaces, we’ll create an interface MediaPlayer that has two methods called play and pause. As mentioned before, all the methods in this interface are abstract. In other words, the interface contains only method declarations.
为了理解抽象和接口,我们将创建一个接口MediaPlayer,它有两个方法,叫做play和pause。如前所述,这个接口中的所有方法都是抽象的。换句话说,这个接口只包含方法的声明。
In Java, interfaces don’t need to explicitly declare a method as abstract or public. The classes that implement the interface MediaPlayer will define these methods:
在Java中,接口不需要明确地将方法声明为抽象或公共。实现接口MediaPlayer的类将定义这些方法。
public interface MediaPlayer {
void play();
void pause();
}
The AudioMediaPlayer class implements MediaPlayer, and it’ll define the play and pause methods for audio media:
AudioMediaPlayer类实现了MediaPlayer,它将为音频媒体定义play和pause方法。
public class AudioMediaPlayer implements MediaPlayer {
@Override
public void play() {
System.out.println("AudioMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("AudioMediaPlayer is Paused");
}
}
Similarly, VideoMediaPlayer implements MediaPlayer and provides a method definition to play and pause video media:
同样地,VideoMediaPlayer实现了MediaPlayer,并提供了一个方法定义来播放和暂停视频媒体。
public class VideoMediaPlayer implements MediaPlayer {
@Override
public void play() {
System.out.println("VideoMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("VideoMediaPlayer is Paused");
}
}
Further, let’s create an instance of AudioMediaPlayer and VideoMediaPlayer and call play and pause methods for both of them:
此外,让我们创建一个AudioMediaPlayer和VideoMediaPlayer的实例,为它们调用play和pause方法。
AudioMediaPlayer audioMediaPlayer = new AudioMediaPlayer();
audioMediaPlayer.play();
audioMediaPlayer.pause();
VideoMediaPlayer videoMediaPlayer = new VideoMediaPlayer();
videoMediaPlayer.play();
videoMediaPlayer.pause();
AudioMediaPlayer and VideoMediaPlayer call their respective implementations of play and pause:
AudioMediaPlayer和VideoMediaPlayer调用各自的play和pause实现。
AudioMediaPlayer is Playing
AudioMediaPlayer is Paused
VideoMediaPlayer is Playing
VideoMediaPlayer is Paused
3.3. Multiple Inheritance
3.3.多重继承
Java doesn’t support multiple inheritance directly due to ambiguity issues. An ambiguity issue occurs when a class inherits from more than one parent class, and both the parent classes have a method or property with the same name. Hence, the child class cannot resolve the conflict of the method or property to be inherited. However, a class can inherit from multiple interfaces. Let’s create an interface AdvancedPlayerOptions:
由于歧义问题,Java并不直接支持多重继承。当一个类继承自一个以上的父类,而两个父类都有一个同名的方法或属性时,就会出现歧义问题。因此,子类无法解决要继承的方法或属性的冲突。然而,一个类可以继承自多个接口。让我们创建一个接口AdvancedPlayerOptions。
public interface AdvancedPlayerOptions {
void seek();
void fastForward();
}
Class MultiMediaPlayer implements MediaPlayer and AdvancedPlayerOptions and defines the methods declared in both the interfaces:
类MultiMediaPlayer实现MediaPlayer和AdvancedPlayerOptions,并定义了两个接口中声明的方法。
public class MultiMediaPlayer implements MediaPlayer, AdvancedPlayerOptions {
@Override
public void play() {
System.out.println("MultiMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("MultiMediaPlayer is Paused");
}
@Override
public void seek() {
System.out.println("MultiMediaPlayer is being seeked");
}
@Override
public void fastForward() {
System.out.println("MultiMediaPlayer is being fast forwarded");
}
}
Now, we’ll create an instance of MultiMediaPlayer class and call all the implemented methods:
现在,我们将创建一个MultiMediaPlayer类的实例并调用所有实现的方法。
MultiMediaPlayer multiMediaPlayer = new MultiMediaPlayer();
multiMediaPlayer.play();
multiMediaPlayer.pause();
multiMediaPlayer.seek();
multiMediaPlayer.fastForward();
As expected, MultiMediaPlayer calls its implementations of play and pause:
正如预期的那样,MultiMediaPlayer调用其play和pause的实现。
MultiMediaPlayer is Playing
MultiMediaPlayer is Paused
MultiMediaPlayer is being seeked
MultiMediaPlayer is being fast forwarded
4. Conclusion
4.总结
In this tutorial, we discussed the significant differences between extends and implements. Furthermore, we created classes and interfaces to demonstrate the concepts of extends and implements. Also, we discussed multiple inheritance and how we can achieve it using interfaces.
在本教程中,我们讨论了extends和implements之间的显著区别。此外,我们创建了类和接口来演示extends和implements的概念。此外,我们还讨论了多重继承以及我们如何使用接口来实现它。
This implementation is available over on GitHub.
该实施方案可在GitHub上获得。