Java Interface

In Java, the multiple inheritance problem is solved with a powerful construct called interfaces. Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface.

Interfaces just specify the method declaration (implicitly public and abstract) and can only contain fields (implicitly public, static and final). Interface definition begins with a keyword interface. An interface like that of an abstract class cannot be instantiated. It is basically used to achieve abstraction.

Multiple Inheritance is allowed when extending interfaces i.e. one interface can extend none, one or more interfaces. It also represents the IS-A relationship. Java classes do not support multiple inheritance, but it allows us to extend one class and implement many interfaces.

 

Example 1

Below is an example of a Shape interface which is being implemented by Rectangle, Circle and Square classes.

InterfaceTest.java

interface Shape {
double PI = 3.14;
double computeArea(double length, double breadth);
}

class Rectangle implements Shape {

public double computeArea(double length, double breadth) {
return length * breadth;
}
}

class Circle implements Shape {

public double computeArea(double length, double breadth) {
return PI * length * length;
}
}

class Square implements Shape {

public double computeArea(double length, double breadth) {
return length * length;
}
}

public class InterfaceTest {

public static void main(String[] args) {
Shape s; //reference
System.out.println("PI = " + Shape.PI); //use like static field
s = new Rectangle();
System.out.println("AREA1 = " + s.computeArea(20, 10));
s = new Circle();
System.out.println("AREA2 = " + s.computeArea(10, 0));
s = new Square();
System.out.println("AREA3 = " + s.computeArea(20, 0));
}
}

Output:

PI = 3.14
AREA1 = 200.0
AREA2 = 314.0
AREA3 = 400.0

NOTE: Interface fields are public, static and final by default, and the methods are public and abstract.

 

Example 2

If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract and the method definitions must be provided by the concrete subclass that extends the abstract class. See the coding example below.

InterfaceExample.java

//interface
interface Grandparent {
int x = 100;
void disp(); //abstract method 1
void show(); //abstract method 2
}

//abstract class
abstract class Parent implements Grandparent {
abstract public void disp(); //abstract method 1
}

//concrete class
class Child extends Parent {

public void disp() {
System.out.println("concrete class");
}

public void show() {
System.out.println("x = " + x);
}
}

public class InterfaceExample {

public static void main(String[] args) {
Grandparent g = new Child();
g.disp();
g.show();
}
}

Output:

concrete class
x = 100

 

Example 3

See another example of using interfaces and classes together.

InterfaceTesting.java

interface X { int a = 10; void disp1(); }

interface Y { int b = 20; void disp2(); }

interface Z extends X, Y { int c = 30; void disp3(); }

class P { int d = 40; }

class Q implements X, Y {
public void disp1() { System.out.println("a = " + a); }
public void disp2() { System.out.println("b = " + b); }
}

class R extends P implements Z {
public void disp1() { System.out.println("a = " + a); }
public void disp2() { System.out.println("b = " + b); }
public void disp3() { System.out.println("c = " + c); }
void show() {
System.out.println("d = " + d);
}
}

//Not allowed in Java
//class R implements Z extends P { }

public class InterfaceTesting {

public static void main(String[] args) {
X x;
Y y;
Z z;
Q q = new Q();
q.disp1();
q.disp2();
System.out.println();
R r = new R();
r.disp1();
r.disp2();
r.disp3();
r.show();
}
}

Output:

a = 10
b = 20

a = 10

b = 20
c = 30
d = 40

 

New Addition

Since Java SE 8, an interface can include default method (to provide default implementation) and static method (to be accessed directly by interface) in it.

See the following example.

InterfaceNew.java

interface Sample {
int x = 100; //field

//abstract method
void show();

//default method
default void print() {
System.out.println("interface default method");
}

//static method
static int square(int x) {
return x * x;
}

}

class NewClass implements Sample {

public void show() {
System.out.println("Value of x : " + x);
}

}

public class InterfaceNew {

public static void main(String[] args) {
NewClass ob = new NewClass();
ob.show();
ob.print();
System.out.println("Square value : " + Sample.square(14));
}
}

Output:

Value of x : 100
interface default method
Square value : 196

 

From Java SE 9, an interface can also include private method and private static method in it. The idea is to share common code and to enhance code reusability.

See the coding example below.

TestNewInterface.java

interface Test {

//default method
default void check() {
pvtMethod(); // calling private method
pvtStaticMethod(); // calling private static method
}

// private method
private void pvtMethod() {
System.out.println("private method called");
}

// private static method
private static void pvtStaticMethod() {
System.out.println("private static method called");
}
}

public class TestNewInterface implements Test {

public static void main(String[] args) {
Test t = new TestNewInterface();
t.check();
}
}

Output:

private method called
private static method called

 

Difference between abstract class and interface

Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can’t be instantiated.

But there are many differences between abstract class and interface which are listed below.

Abstract classInterface
1) Abstract class doesn’t support multiple inheritance like any other class in Java programming language.1) Interface supports multiple inheritance.
2) Abstract classes can have abstract and non-abstract (i.e. concrete) methods.2) Interfaces have abstract methods only. Since JSE 8 & 9, it now have default, static, private and private static methods also.
3) Abstract class can have final, non-final, static and non-static fields.3) Interface has only static and final fields.
4) Abstract class can provide the implementation of interface.4) Interface can’t provide the implementation of abstract class.
5) The abstract keyword is used to define abstract class.5) The interface keyword is used to define interface.
6) An abstract class can extend another class and implement multiple interfaces.6) An interface can extend another interface only.
7) An abstract class can be extended using keyword “extends“.7) An interface can be implemented using keyword “implements“.
8) An abstract class can have members like private, protected, etc.8) Members of a interface are public by default.
9) Example:
abstract class Shape {
      abstract public void area();
}
9) Example:
interface Figure {
      void area();
}

Basically, an abstract class achieves partial abstraction (0 to 100%) whereas an interface achieves full abstraction (100%).