Interface Vs Abstract Class In Java

Java is among the most popular Object-Oriented Programming (OOP) languages and widely used by enterprises to build reliable, secure, and maintainable software. Interfaces and Classes are the most important aspect of any OOP language and considered as the base for designing good software that can be easily understood, extended and managed by others. This tutorial explains the differences between an Interface and Abstract Class with examples.

We can use both the Interfaces and Abstract classes for abstraction to leave the actual implementation by just designing the prototype. The actual implementation can be done by the classes implementing the interfaces and the abstract classes. An Interface or Abstract Class cannot be considered as fully implemented until all the abstract methods are defined.

Interface Examples

An Interface can be considered as the prototype or blueprint to be implemented by the classes. The interfaces can only declare methods without any definition part as shown below. I have covered the important concepts of Interfaces in these examples.

// An Interface without methods, showing globals with appropriate modifiers

public interface A {



// Constant with public and final modifiers

public final int GLOBAL_A = 10;



// Constant without public modifier

final int GLOBAL_B = 20;



// Constant without final modifier

public int GLOBAL_C = 30;



// Constant without public and final modifiers

int GLOBAL_D = 40;

}



// An Interface with methods declaration only with appropriate modifiers

public interface B {



// Abstract Method with public and abstract modifiers

public abstract int add( int a, int b );



// Abstract Method without public modifier

abstract int sub( int a, int b );



// Abstract Method without abstract modifier

public int divide( int a, int b );



// Abstract Method without public and abstract modifiers

int multiply( int a, int b );

}



// An Interface showing single inheritance and polymorphism

public interface C extends A {



// Abstract Method with public and abstract modifiers

public abstract int add( int a, int b );



// Overloaded method

public abstract int add( int a, int b, int c );

}



// An Interface showing multiple inheritance and polymorphism

public interface D extends A, B {



// Overridden method

int sub( int a, int b );



// Overloaded method

int sub( int a, int b, int c );

}

Abstract Class Examples

A class can be considered as an Abstract Class if it contains the abstract keyword in its declaration. We cannot create objects of Abstract Classes. The Abstract Class can declare abstract methods, but a concrete class without the abstract keyword in its declaration cannot declare abstract methods. The below examples show the abstract classes.

// An Abstract Class without methods

public abstract class CA implements A {



// Constant with public and final modifiers

public final int GLOBAL_E = 50;



// Static Variable

public static int f = 60;



// Public Member Variable

public int g = 70;



// Protected Member Variable

protected int h = 80;



// Private Member Variable

private int i = 90;

}



// An Abstract Class with abstract and non abstract methods

public abstract class CB implements B {



// Abstract Method with public and abstract modifiers

public abstract int add( int a, int b );



// Public Method

public int sub( int a, int b ) {



return a - b;

}



// Protected and Overloaded Method

protected int add( int a, int b, int c, int d ) {



return a + b + c + d;

}



// Private and Overloaded Method

private int add( int a, int b, int c, int d, int e ) {



return a + b + c + d + e;

}

}



// An Abstract Class showing multiple inheritance

public abstract class CC implements A, B {



// Method with definition

public int add( int a, int b ) {



return a + b;

}



// Method with definition

public int sub( int a, int b ) {



return a - b;

}

}



// An Abstract Class showing multiple inheritance

public abstract class CD extends CC implements C, D {



// Method with definition

public int add( int a, int b, int c ) {



return a + b + c;

}



// Method with definition

public int sub( int a, int b, int c ) {



return a - b - c;

}

}



// Concrete Class

public class CE extends CD {



// Method with definition, throws exception

public int divide(int a, int b) throws ArithmeticException {



return a / b;

}



// Method with definition

public int multiply(int a, int b) {



return a * b;

}

}

Declaration

The important points to be taken care of while declaring an Interface or Abstract Class are listed below.

Both the Interface and Abstract Class are permitted to use the abstract and public modifiers.

It's optional for an Interface to use the abstract modifier, whereas an Abstract Class must always use the abstract modifier. All the Interfaces are abstract by default.

Both the Interface and Abstract Class cannot use the private modifier.

Both the Interface and Abstract Class cannot use the final modifier which contradicts their abstraction.

Variables

The variables declared in an Interface are inherently public and final as shown in the Interface A.

The variables declared in an Abstract Class can be final, static, public, protected, or private as shown in the Class CA.

Methods

The methods declared in an Interface are inherently public and abstract as shown in the Interface B. We can also have default and static methods in an Interface since Java 8.

The methods declared in an Abstract Class can be final, static, public, protected, or private as shown in the Class CB. The Abstract Classes can always have abstract and non-abstract methods.

Scope

We cannot declare private or protected Interfaces and Abstract Classes. The Interfaces and Abstract Classes can be either publicly visible within the same and other packages using the public access modifier or visible within the same package without the public access modifier.

Inheritance

An Interface can extend either single or multiple Interfaces.

An Abstract Class can extend only a single Abstract or Concrete Class.

Implementation

An Interface can be implemented by an Abstract Class using the keyword implements.

An Abstract Class can implement multiple Interfaces.

An Abstract Class can extend the Abstract or Concrete Class using the keyword extends.

An Interface cannot implement another Interface or Abstract Class.

Usage

We can use an Abstract Class by partially implementing the class leaving the rest of the implementation to the child classes.

We can use an Interface to declare the methods only, leaving the implementation to the classes in their own way.

We can use an Interface to declare the Global Variables.

We can use Interfaces to achieve multiple inheritance in classes.

Summary

This tutorial provided examples of Interfaces and Classes showing their usage. It also provided a comparison of the classes and interfaces for declaration, variables, functions, scope, inheritance, and implementation.

You are most welcome to join the discussion by sharing your views by submitting comments either directly to Tutorials24x7 or via Disqus.

You can also follow Interface Vs Abstract Class In Java to get the infographics having a comparison of Interface and Abstract Class.