Originally posted at ellehallal.dev👩🏽💻
What Are Interfaces?
Interfaces are used to standardise the way a particular set of classes are used. An interface specifies common behaviour, which needs to be implemented by the classes. It can be described as a blueprint of a class.
An interface decouples what needs to be implemented from how it is implemented. It is not concerned with how the behaviour is implemented.
Every method declared in an interface will need to be included in a class which implements it. The body of methods in an interface are empty, and therefore classes implementing it need to override the methods to add a method body.
An interface contains methods and constants, which are automatically public andabstract. As a result, including these keywords is optional.
For example, here’s the play
method in the Pet
interface. It has an empty method body:
public interface Pet {
void play();
}
Interfaces have an is-a relationship. In the example below where Cat
is implementing Pet
, Cat is-a Pet
:
public class Cat implements Pet {
private String name;
public Cat(String name) {
this.name = name;
}
@Override
public void play() {
System.*out*.println(name + " is playing with human");
}
}
Cat
has overridden the play
method in the Pet
interface, with its own play
method.
Below is another example, using an interface called MediaPlayer
. The classes MusicPlayer
and VideoPlayer
implement the interface.
Interface - MediaPlayer
public interface MediaPlayer {
void play();
void stop();
void currentlyPlaying();
}
Class - MusicPlayer
public class MusicPlayer implements MediaPlayer{
private String songName;
private boolean isPlaying = false;
public MusicPlayer(String songName) {
this.songName = songName
}
@Override
public void play() {
isPlaying = true;
}
@Override
public void stop() {
isPlaying = false;
}
@Override
public void currentlyPlaying() {
if (isPlaying) {
System.out.println(songName + " is currently playing.");
}
else {
System.out.println("Nothing is playing.");
}
}
}
Class - VideoPlayer
public class VideoPlayer implements MediaPlayer{
private String videoName;
private boolean isPlaying = false;
public VideoPlayer(String videoName) {
this.videoName = videoName
}
@Override
public void play() {
System.out.println("Playing...");
isPlaying = true;
}
@Override
public void stop() {
System.out.println("Stopped");
isPlaying = false;
}
@Override
public void currentlyPlaying() {
if (isPlaying) {
System.out.println(videoName + " is currently playing.");
}
else {
System.out.println(videoName + "is not playing. Press play");
}
}
}
MusicPlayer
and VideoPlayer
have implemented the play
, stop
, and currentlyPlaying
methods declared in MediaPlayer
, by overriding them.
Other key points
- An interface cannot be instantiated
- A single class can implement more than one interface
- Multiple classes can implement the same interface
- An interface can extend another interface
- Another example of an interface is List in Java.
What Are Abstract Classes?
The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
It should be used when a class contains some implementation code, which all subclasses can use, such as a method with a body. Like interfaces, an abstract class can also be referred to as a blueprint.
An abstract class contains at least one abstract method. An abstract method is a method with an empty body, just like the methods in an interface.
The difference is in an abstract class, the abstract keyword needs to be used when declaring the class and abstract methods. Here’s an example:
public abstract class Animal {
public abstract void speak();
}
In addition, methods within this class type can have any visibility, where as in an interface, methods are public only.
Abstract classes can also contain non-abstract methods, meaning the method body is defined. For example, here’s the abstract class, Animal
, with the non-abstract method age:
public abstract class Animal {
public abstract void speak();
public void age(int age) {
System.out.println("I am " + age + " years old");
}
}
Abstract classes cannot be instantiated, and classes which extend from it need to override the abstract methods to provide implementation. Below is an example of a class which extends from the Animal
class, overriding the abstract method speak:
public class Dog extends Animal {
@Override
public void speak() {
System.*out*.println("Woof!");
}
}
An abstract class can implement an interface. In addition, a subclass of an abstract class usually provides implementations of all abstract methods from the parent class. If not, the subclass must also be declared as abstract.
What Are Concrete Classes?
A concrete class implements all of its inherited methods and state from an interface and/or an abstract class. Unlike an interface or abstract class, a concrete class can be instantiated.
It demonstrates the implementation of a blueprint. Any abstract methods are overridden, to include a method body.
A concrete class can implement multiple interfaces, but can only inherit from one parent class.
In the example below, the Dog
class inherits its methods from the interface, Pet
, and the abstract class, Animal
.
Another example of a concrete class is an ArrayList in Java. It implements the methods in the List interface. It also extends from the AbstractList class. In addition, it implements other interfaces, such as Iterable and Cloneable.
Top comments (0)