What Are Interfaces, Abstract And Concrete Classes?

3 min read

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:

1public interface Pet {
2 void play();

Interfaces have an is-a relationship. In the example below where Cat is implementing Pet, Cat is-a Pet:

1public class Cat implements Pet {
2 private String name;
4 public Cat(String name) {
5 this.name = name;
6 }
8 @Override
9 public void play() {
10 System.*out*.println(name + " is playing with human");
11 }

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

1public interface MediaPlayer {
2 void play();
3 void stop();
4 void currentlyPlaying();

Class - MusicPlayer

1public class MusicPlayer implements MediaPlayer{
2 private String songName;
3 private boolean isPlaying = false;
5 public MusicPlayer(String songName) {
6 this.songName = songName
7 }
9 @Override
10 public void play() {
11 isPlaying = true;
12 }
14 @Override
15 public void stop() {
16 isPlaying = false;
17 }
19 @Override
20 public void currentlyPlaying() {
21 if (isPlaying) {
22 System.out.println(songName + " is currently playing.");
23 }
24 else {
25 System.out.println("Nothing is playing.");
26 }
27 }

Class - VideoPlayer

1public class VideoPlayer implements MediaPlayer{
2 private String videoName;
3 private boolean isPlaying = false;
5 public VideoPlayer(String videoName) {
6 this.videoName = videoName
7 }
9 @Override
10 public void play() {
11 System.out.println("Playing...");
12 isPlaying = true;
13 }
15 @Override
16 public void stop() {
17 System.out.println("Stopped");
18 isPlaying = false;
19 }
21 @Override
22 public void currentlyPlaying() {
23 if (isPlaying) {
24 System.out.println(videoName + " is currently playing.");
25 }
26 else {
27 System.out.println(videoName + "is not playing. Press play");
28 }
29 }

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:

1public abstract class Animal {
2 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:

1public abstract class Animal {
2 public abstract void speak();
4 public void age(int age) {
5 System.out.println("I am " + age + " years old");
6 }

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:

1public class Dog extends Animal {
3 @Override
4 public void speak() {
5 System.*out*.println("Woof!");
6 }

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.

Previous post:
What Is The Liskov Substitution Principle?
Next post:
What Is The Template Method Design Pattern?