Abstract Vs Interface

Abstract and Interfaces are two important uses in Java Classes. Here are a few differences between the two of them. At the bottom I put an example of each one being used.

Abstract classes are allowed to have both abstract and non abstract methods. While interface methods are ONLY allowed to have abstract methods.

Abstract

abstract class LaptopsAbstract
{
    public void color(String aColor)
    {
        System.out.println("The laptop color is " + aColor);
    }

    abstract public void type(String aType);
}

Interface

interface LaptopInterface
{
    abstract public void color(String aColor);

    abstract public void type(String aType);
}

Abstract classes DO NOT support multiple inheritance. While Interface classes do allow it.

class Mac implements LaptopInterface, LaptopInterfaceTwo
{

When using Interface classes you have to implement. Using abstract classes you must extend.

Interface

class Mac implements LaptopInterface
{}

Abstract

class Acer extends LaptopsAbstract
{}

Abstract classes can have final, non final, static and non static variables. Interface has only static final variables.

Abstract

abstract class LaptopsAbstract
{
    String color = "black";
    Final int screenSize = 13;
}

Interface

interface LaptopInterface
{
     Static Final int screenSize = 13;
}

An Interface can only extend another interface. An Abstract can extend another class and implement multiple interfaces.

Interface

interface LaptopInterface extends LaptopInterfaceTwo
{}

Abstract

abstract class LaptopsAbstract extends OtherClass implements LaptopInterface
{}

Abstract can have private protected etc. methods. While Interface is only public by default.

Abstract

abstract class LaptopsAbstract
{
    private String color = "black";
}

Interface

interface LaptopInterface
{
    String color = "black";
}

Below is an example of extending Abstract classes

abstract class LaptopsAbstract
{
    private String color = "black";

    public void color(String aColor)
    {
        System.out.println("The laptop color is " + aColor);
    }

    abstract public void type(String aType);

    abstract public void screenSize(int screenSize);
}

class Acer extends LaptopsAbstract
{
    public void type(String aType)
    {
        System.out.println("The tpe of laptop is " + aType);
    }

    public void screenSize(int screenSize)
    {
        System.out.println("The screen size of the laptop is " + screenSize);
    }

    public static void main(String[] args) {
        LaptopsAbstract acer = new Acer();

        acer.type("Acer");
        acer.screenSize(17);
        acer.color("black");
    }
}

Below is an example implementing Interfaces

interface LaptopInterface
{
    String color = "black";

    abstract public void color(String aColor);

    abstract public void type(String aType);

    abstract public void screenSize(int screenSize);
}

class Mac implements LaptopInterface
{
    public void color(String aColor)
    {
        System.out.println("The color is " + aColor);
    }

    public void type(String aType)
    {
        System.out.println("The type is " + aType);
    }

    public void screenSize(int screenSize)
    {
        System.out.println("The screen size is " + screenSize);
    }

    public static void main(String[] args) {
        LaptopInterface aMac = new Mac();

        aMac.color("White");
        aMac.type("Mac");
        aMac.screenSize(13);
    }
}

Leave a comment

Design a site like this with WordPress.com
Get started