Interfaces in Java

Please read this post after reading our previous post on abstract classes.

Abstract Classes in Java

Since Java does not have multiple inheritance, a class cannot inherit the properties of more than one class. In such cases interfaces are used. An interface does not lie in the inheritance chain. Therefore the interface cannot allocate memory. That means that the interface must have all it’s memory requirements fixed at compile time. Therefore an interface can only have abstract methods and static constants.

public interface InterfaceExample
{
public int x1=1;
public static int x2=2;
public static int x3;
public static final int x4=4;
}
Try compiling:-

interfaces in java

 

It demands a value for the x3 variable.This demonstrates that it is a constant.
Develop a new class that implements the interface:-
public class InterfaceImplementation implements InterfaceExample
{
public static void main(String[] args)
{
InterfaceImplementation i1=new InterfaceImplementation();
InterfaceImplementation i2=new InterfaceImplementation();
System.out.println(InterfaceImplementation.x1);
System.out.println(InterfaceImplementation.x2);
System.out.println(InterfaceImplementation.x4);
System.out.println(i1.x1);
System.out.println(i1.x2);
System.out.println(i1.x4);
System.out.println(i2.x1);
System.out.println(i2.x2);
System.out.println(i2.x4);
}
}

interfaces in java

interfaces in java

 

The outputs are the same for each case. Now, try changing the values of x1,x2 and x4;

public class InterfaceImplementation implements InterfaceExample
{
public static void main(String[] args)
{
InterfaceImplementation i1=new InterfaceImplementation();
InterfaceImplementation i2=new InterfaceImplementation();
System.out.println(InterfaceImplementation.x1);
System.out.println(InterfaceImplementation.x2);
System.out.println(InterfaceImplementation.x4);
System.out.println(i1.x1);
System.out.println(i1.x2);
System.out.println(i1.x4);
System.out.println(i2.x1);
System.out.println(i2.x2);
System.out.println(i2.x4);
InterfaceImplementation.x1=10;
InterfaceImplementation.x2=10;
InterfaceImplementation.x4=10;
}
}

interfaces in java

 

This proves that all variables in interfaces are actually static and final. Whether they are marked as such, or not.

Now, we implement some methods.

public interface InterfaceExample
{

public int x1=1;
public static int x2=2;
public static final int x4=4;
public void f1()
{
}
public abstract void f2();
public static void f3()
{
}
public abstract static void f4();
}

The compilation output.
interfaces in java

 

Next remove the errors:-

public interface InterfaceExample
{
public int x1=1;
public static int x2=2;
public static final int x4=4;
public abstract void f2();
}

The interface now compiles.

interfaces in java

 

public interface InterfaceExample
{
protected int x1=1;
private static int x2=2;
public static final int x4=4;
public abstract void f2();
}

interfaces in java

 

Now, the final shape of the InterfaceExample

public interface InterfaceExample
{
int x1=1;
static int x2=2;
public static final int x4=4;
public abstract void f2();
public void f3();
}

 

It compiles.

interfaces in java

 

The listing using javap

interfaces in java

 

This shows that all variables become static, public and final. While all methods become public ,instance and abstract.

Properties of Interfaces:-

  • Interfaces have only public members.
  • Interfaces do not allow variables, only constants are allowed.
  • Interfaces only allow static constants
  • No concrete methods are allowed.
  • Methods marked as abstract, or not marked as abstract are all abstract.
  • Members can be marked as public, or left unmarked. All of them will be public.

 

Now, we develop an interface called IAngle which gives the angle inside an enclosed figure.

public interface IAngle
{
public int getEnclosedAngle();
}

Next modify the Rectangle class.

public class Rectangle extends Figure implements IAngle
{
private double length,breadth;
public Rectangle(double length,double breadth)
{
this.length=length;
this.breadth=breadth;
}
public double area()
{
return length*breadth;
}
public double perimeter()
{
return 2*length + 2*breadth;
}
public double diagonal()
{
return Math.sqrt(length*length + breadth*breadth);
}
}

Try compiling the Rectangle class.
interfaces in java

 

Along with the abstract class methods, the interface methods too have to be implemented.

 

public class Rectangle extends Figure implements IAngle
{
private double length,breadth;
public int getEnclosedAngle()
{
return 360;
}
public Rectangle(double length,double breadth)
{
this.length=length;
this.breadth=breadth;
}
public double area()
{
return length*breadth;
}
public double perimeter()
{
return 2*length + 2*breadth;
}
public double diagonal()
{
return Math.sqrt(length*length + breadth*breadth);
}
}

Try compiling:-

interfaces in java

 

Now, we have another way of creating the Rectangle class. Change AbstractUse.java

 

public class AbstractUse
{
public static void main(String[] args)
{
Figure f1=new Rectangle(3,4);
Figure f2=new Circle(7);
Rectangle r1=new Rectangle(3,4);
Circle c1=new Circle(7);
System.out.println(f1.area());
System.out.println(f1.perimeter());
System.out.println(f1.degreeToRadians(180));
//System.out.println(f1.diagonal());
System.out.println(r1.area());
System.out.println(r1.perimeter());
System.out.println(r1.degreeToRadians(180));
System.out.println(r1.diagonal());
IAngle a=new Rectangle(3,4);
System.out.println(a.area());
System.out.println(a.perimeter());
System.out.println(a.degreeToRadians(180));
System.out.println(a.diagonal());
System.out.println(a.getEnclosedAngle());
}

interfaces in java

 

It only recognizes it’s own method.Comment the rest, compile and run:-

public class AbstractUse
{
public static void main(String[] args)
{
Figure f1=new Rectangle(3,4);
Figure f2=new Circle(7);
Rectangle r1=new Rectangle(3,4);
Circle c1=new Circle(7);
System.out.println(f1.area());
System.out.println(f1.perimeter());
System.out.println(f1.degreeToRadians(180));
//System.out.println(f1.diagonal());
System.out.println(r1.area());
System.out.println(r1.perimeter());
System.out.println(r1.degreeToRadians(180));
System.out.println(r1.diagonal());
IAngle a=new Rectangle(3,4);
//System.out.println(a.area());
//System.out.println(a.perimeter());
//System.out.println(a.degreeToRadians(180));
//System.out.println(a.diagonal());
System.out.println(a.getEnclosedAngle());
}
}

interfaces in java

The javap listings for each class and interface:-

interfaces in java

 interfaces in java

interfaces in java

interfaces in java

 

Any number of interfaces can be implemented, while only one class can be extended.

 

 

 

 

 

 

Leave a Reply