What Are Interfaces, Abstract and Concrete Classes?
Tags: oop, interfaces, classes, cleancode
Published: 2020-09-01 12:18:44

---
title: What Are Interfaces, Abstract and Concrete Classes?
published: true
date: 2019-05-29 00:00:00 UTC
tags: #OOP, #interfaces, #classes, #cleancode
canonical_url: https://ellehallal.dev/blog/2019/05/2019-05-29-what-are-interfaces-abstract-classes-and-concrete-classes/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/1mpzf2rh5vtpwez12zub.png
---

<center><h3>Originally posted at <a href="https://ellehallal.dev" target="_blank">ellehallal.dev</a>👩🏽‍💻</h3></center>

---

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

```java
public interface Pet {
  void play();
}
```

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

```java
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**

```java
public interface MediaPlayer {
    void play();
    void stop();
    void currentlyPlaying();
}
```

**Class - MusicPlayer**

```java
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**

```java
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](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) 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:

```java
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:

```java
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:

```java
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`.

![](https://i.imgur.com/9w7cCGl.png)

Another example of a concrete class is an ArrayList in Java. It implements the methods in the [List](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) interface. It also extends from the [AbstractList](https://docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html) class. In addition, it implements other interfaces, such as Iterable and Cloneable.