In this post, you will learn to:
- Discuss the concept of interfaces.
- Describe how to use interfaces.
- Explain extending interfaces.
- Explain IS-A relationship.
Introduction to Interfaces
An interface in Java is a contract that lays down rules to be followed by the types which implement it. Consider a new employee who joins an organization to work with them usually signs a contract which states that he/she will abide by the rules laid down by the organization and comply with their guidelines.
In the programming world too, a contract can be defined for classes, such that once they accept the contract they will abide by it. Such a contract is an interface.
Implementing Interfaces
An interface in Java is defined as a reference type and is similar to a class except that it has only final and static variables, abstract method signatures. An interface cannot be instantiated. It can be implemented by other classes or extended by other interfaces. When an interface is implemented by a class, all the variables of the interface will act as constant variables for the class. In other words, they cannot be changed by the implementing class.
A class that implements an interface is required to provide implementations for all the methods of the interface or else should be declared abstract. For example, if an interface has four methods, and a class implements only three methods, then the class must be declared as abstract.
The following is the syntax for declaring an interface.
<access_specifier> interface <interface_name> { // static final variable declarations // abstract method declarations }
The following code demonstrates the implementation of an interface, Numbers.
interface Numbers { final int maxnum=3; public void printNums(); } // Class implementing interface, Numbers class IntegerNum implements Numbers { public void printNums(){ for(int i = 0;i < maxnum; i++) System.out.println("Value of number is " + i); } } class FloatNum implements Numbers { public void printNums(){ for(float i = 0.0f; i<maxnum; i++) System.out.println("Value of number is " + i); } } class DisplayNum { public static void main(String args[]) { IntegerNum obj1 = new IntegerNum(); obj1.printNums(); FloatNum obj2 = new FloatNum(); obj2.printNums(); } }
As shown in the code, the interface Numbers is implemented by two classes, IntegerNum and FloatNum. These two classes provide an implementation for the printNums() method which is declared in the interface.
Output:
Value of number is 0 Value of number is 1 Value of number is 2 Value of number is 0.0 Value of number is 1.0 Value of number is 2.0
Interfaces can be Extended
In Java, a simple class can extend another class, which in turn extends a third class and so on. This concept known as inheritance can also be used for interfaces. An interface is an abstract class. Hence, an interface can also extend another interface just like a class extends another java class. In fact, the Java language exhibits multiple-inheritance only in case of interfaces. This means that a single interface can implement more than one interface.
The following code demonstrates extending of an interface.
interface Game extends Player, Animation { // Variables and method declarations of both Player and Animation interfaces available here. }
Here, the interface Game implements the interfaces Player and Animation both. Hence, it can be said that the interface Game inherits the methods of the interfaces Player and Animation both.
IS-A Relationship
It is a concept based on class inheritance or interface implementation. An IS-A relationship expresses class hierarchy in case of class inheritance. For example, if a class Ferrari extends the class Car, then the statement ‘Ferrari IS-A Car is true’. If the class Car itself extends another class Vehicle, then the relationship ‘Ferrari IS-A Vehicle’ is also true.
The figure below depicts an IS-A relationship which can be interpreted as an arrow in a graphic depicting class hierarchy.
The IS-A relationship can also be used in the case of interface implementations. An IS-A relationship in case of interfaces is expressed in Java with the keyword implements. A Java class can implement multiple interfaces. This is called multiple-interface-inheritance in Java. It is used to force a subclass to follow user-defined rules on overridden methods.