Interfaces, Abstract Classes and Concrete Classes

Elle Hallal
4 min readMay 29, 2019

🎉 You can find new and updated posts at ellehallal.dev

This is a quick blog on my understanding of interfaces, abstract classes and concrete classes. There are still elements I am unsure about, so please feel free to leave feedback.

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 implements the interface.

Interface - MediaPlayer

Class - MusicPlayer

Class - VideoPlayer

MusicPlayer and VideoPlayer have implemented the play , stop, and currentlyPlaying methods declared inMediaPlayer, 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.

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.

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.

--

--

Elle Hallal

This blog is no longer updated. Please visit → https://ellehallal.dev Software Crafter at 8th Light. Leadership Team Member & Mentor at Coding Black Females