top of page
Search

Interface vs Abstract Class

  • Writer: Hanno Van Der Walt
    Hanno Van Der Walt
  • Jan 26, 2023
  • 5 min read

Updated: Jan 30, 2023


In this article, we will be looking at the difference between an interface and an abstract class. All software developers need to know the differences so that they can effectively design and build applications. Let's look at the definitions as well as some practical examples to understand the differences.


 

Interfaces

Let us start by looking at the interface. An interface is a contract or a “blueprint”. It allows you to define abstract methods which are method signatures with no body. Interfaces are not allowed to contain any instance variables but are allowed to have constants. A class can implement multiple interfaces and should implement all the functions that were declared in the interface. Interfaces do not make use of access modifiers and default to public. Interfaces cannot be instantiated directly and must be implemented by another class. An interface cannot contain any constructors.

When to use an Interface

Interfaces should be used when the only common thing between the implementing classes is the method signature. Each implementing class will have its own implementation of the method. Remember that an interface cannot implement data fields like constructors and variables. Also, note that a class can implement multiple interfaces. A useful way of determining if an action is appropriate for an interface is to use the “is able to” rule. For example, a dog “is able to” communicate or a beetle “is able to” feed.


Abstract Classes

Abstract classes allow you to have functionality within your classes, like methods with bodies, these methods are called concrete methods. You can also add abstract methods to an abstract class and give them functionality in the extending class. Make sure to provide implementations in the class that extends the abstract class’s abstract methods. Similar to Interfaces, abstract classes can't be instantiated directly and a class can only extend a single abstract class. An abstract class can contain constructors, variables, and concrete classes and can have access modifiers.

When to use an Abstract Class

Abstract classes should be used when there is a common functionality between the implementing classes. Abstract classes allow for the reuse of code, eliminating the use to re-write repetitive code. Abstract classes also serve abstraction using abstract functions. Abstract classes can implement data fields like variables and constructors to inject services into the abstract class etc. A useful way of determining if an action is appropriate for an abstract class is to use the “is a” rule. For example, a dog “is a” mammal and a bee “is an” insect.


 

Practical Java example

Lets start by creating an abstract class called Mammal.

It is important to note the following about the abstract class:

  • The abstract class can contain a constructor. It is also allowed to contain other variables and constants.

  • The giveBirth() function contains a body (concrete method). All mammals give birth in the same way (live birth) so it is appropriate to put it as a concrete method in an abstract class.

  • The abstract class contains an abstract method called clean() and we can implement it in the class that extends it. Each mammal will get cleaned uniquely.


public abstract class Mammal {

    Mammal(){
        System.out.println("Mammal constructor");
    }

    public void giveBirth(){
        System.out.println("I am a mammal and I give live birth.");
    }

    public abstract void clean();

}

Next, let's make some interfaces that we can implement later. Let's make a Communicate and a Feed Interface. Note the following about the interfaces:

  • Both the sayHello() and eat() methods are not allowed to have bodies.

  • It is allowed to have a static final constant called message.

  • It is not allowed to have any constructors.

Comunicate interface :

public interface Comunicate {

    static final String message = "Hello";

    void sayHello();

}

Feed interface :


public interface Feed {

    void eat();

}

Next, let us make two classes called Dog and Bear. Note the following important points:

  • both of these are mammals and the “is a” rule applies to them. A bear “is a” mammal and a dog “is a” mammal, so it is appropriate to extend the Mammal abstract class.

  • Note that we implemented both the Communicate and Feed Interfaces. Using the “is able to” rule, we can see that a Dog “is able to” communicate and a Bear “is able to” feed.

  • We also had to implement all the abstract methods that were declared in the Feed and Communicate interfaces. Because bears and dogs communicate and feed differently, it is appropriate to add the abstract sayHello() and eat() methods in an interface and implement it in the child classes.

  • Note that if we had a class called bee or butterfly, we can also implement the feed and communicate interfaces and have their unique implementations. Extending the Mammal abstract class from a butterfly class would not make much sense because a butterfly cant give live birth.

  • We can extend only one abstract class (mammal)

  • We can implement multiple interfaces (Communicate, Feed)

  • Abstract methods like the clean() method (methods with no bodies) can also be declared in an abstract class and be implemented in the extending class.


Dog class :

public class Dog extends Mammal implements Comunicate, Feed {

    @Override
    public void sayHello() {

        System.out.println(message + " in Dog language = Woof Woof.");

    }

    @Override
    public void eat() {
        System.out.println("I am a dog and eat dog food.");
    }

    @Override
    public void clean() {
        System.out.println("I make my owner clean me");
    }
}

Bear class :


public class Bear extends Mammal implements Comunicate, Feed{

    @Override
    public void sayHello() {

        System.out.println(message + " in Bear language = Roarrr.");

    }

    @Override
    public void eat() {
        System.out.println("I am a Bear and i eat deer meat.");
    }

    @Override
    public void clean() {
        System.out.println("I bathe myself");
    }
}


Create the main method and create a Dog and Bear object. We can call the functions we created in the interfaces and abstract class to see the output.


Main method class :


public class InterfaceVsAbstract {

    public static void main (String[] args){

        Dog dog = new Dog();
        Bear bear = new Bear();

        dog.sayHello();
        dog.giveBirth();
        dog.eat();
        dog.clean();

        bear.sayHello();
        bear.giveBirth();
        bear.eat();
        bear.clean();

    }

}

Note the following about the output :

  • The sayHello() and eat() functions both outputs something different based on the class it comes from. The function from the interface has different functionality in each class.

  • The giveBirth() concrete function outputs the same value and is because it extends the same abstract class. This is an example of how code can be reused implementing abstract classes.

  • The clean() function gives different outputs because it is also an abstract function (in the abstract class this time). It is implemented differently in the child classes. This shows that abstract classes can have both concrete and abstract functions.

Output :

Hello in Dog language = Woof Woof.
I am a dog and eat dog food.
I am a mammal and I give live birth.
I make my owner clean me

Hello in Bear language = Roarrr.
I am a Bear and I eat deer meat.
I am a mammal and I give live birth.
I bathe myself


 
 
 

コメント


  • LinkedIn
bottom of page