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.

 

Interface-color

 

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.

 

uml_interface_realization_relationship

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);
	}
}

 

Exercise

Which statements are true about the following code?

public interface MyInterface {

	int SMALL_NUMBER;
	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.


Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.

Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.
 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

1 Comment

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017