Java interface implementation

There are some similarities between abstract classes and interfaces. In Java, you can only inherit from a single class, but it is allowed to inherit from one and more interfaces.

When a group classes inherit an abstract class, they all must be related. For example, circles, rectangles and squares are a specific shape and they all have areas and perimeters. That is why they can inherit the class shape and become its subclass. However, that is not exactly the case by interfaces, because many unrelated classes can implement the same interface.




What is an interface in Java?

Java offers interfaces to provide a solution for objects that have multiple behaviors. Here below are some important points to understand interfaces.

General information and rules apply to interfaces

  • Classes don’t have to be related to implement the same interface unlike the related subclasses that extend a single superclass.
  • It is not allowed in Java to inherit from multiple classes, but interfaces offer the advantage of a multiple inheritance.
  • Interfaces can only be implemented by classes or extended by other interfaces. It can not be used to instantiate objects.

Interface methods

  • A class that implements an interface must define all the methods of that interface, otherwise it can not be compiled.
  • All the methods in an interface are implicitly abstract. However, the methods that are declared static or default do have a body.
  • All the overridden methods of an interface that implemented by a class should be also public.

Interface constants

  • Constants of an interface are by default public, static and final even if no modifier is mentioned.
  • All the constants of an interface must be initialized.
  • The modifiers abstract, private and protected are not allowed to use by interface constants.


Interface UML diagram – realization relationship

In the UML diagram below, the class Land implements the interface UnitInterface. The Land class must define the abstract methods inside the interface.



The Java code of the above UML relationship

In some unrelated classes, we need to use units as m², m, cm, ft ..etc. The classes might not be related with each other, but they still can implement the interface UnitInterface.

When this code compile and run, It writes the following to the standard output.

Area: 5000 m²
Perimeter: 300 m


public interface UnitInterface {

	String AREA_UNIT_M = "m²";
	String LENGTH_UNIT_M = "m";
	String AREA_UNIT_CM = "cm²";
	String LENGTH_UNIT_CM = "cm";

	public int getArea(int length, int width);
	public int getPerimeter(int length, int width);

public class Land implements UnitInterface {

	private int length;
	private int width;

	public int getArea(int length, int width){
		return length * width;
	public int getPerimeter(int length, int width){
		return 2 * width + 2 * length;
	public static void main(String[] args){
		Land land = new Land();
		System.out.println("Area: " + land.getArea(100,50) + " " + AREA_UNIT_M);
		System.out.println("Perimeter: " + land.getPerimeter(100,50) + " " + LENGTH_UNIT_M);



Which statements are true about the following code?

public interface MyInterface {

	protected double NUMBER = 20.0;
	int MY_MYNUMBER = 10;

	int getSum(int nr1, int nr2);
	private int getMultiply(int nr1, int nr2);
	int getMultiply(int nr1, int nr2, int nr3);

public class MyClass implements MyInterface {

	int getSum(int a, int b){
		return a + b;
	public int getMultiply(int a, int b, int c){
		return a * b * c;
	public double getPI(){
		return 3.14;

Select all the correct answers.

Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

1 Comment

Add a Comment
  1. Damn, I wish I could think of somitheng smart like that!

Leave a Reply