Search This Blog

Thursday, 22 December 2022

Defining and Implementing Interfaces in Java

0 comments

A class is a container for both data and methods. The public methods of a class are also called as interfaces to the private data (instance variables) hidden withi in a class.

An abstract class in Java, is an abstract one, i.e., it includes abstract methods as a part of it.   An abstract method has only the signature part of a method, without implementation (the body).  Hence, an abstract class can't be used for object creation.  Another class that extends the abstract class must provide the implementation of each of the abstract class defined in the abstract class before object creation.

An interface is similar to an abstract class, but contains only abstract methods and one or more final (static) variables.  Through interface, Java abstracts (separates) the implementation part of all methods defined in an interface from their implementation.  Thus, an interface specifies what must be done, but not how.

In Java, interface is a structure like class that contains only the method signatures (like an abstract class) but not the data.  Using interface, we can specify what a class must do, with out specifying how it should be implemented. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.

Unlike classes, multiple interfaces can be used for deriving a new class that will implement all its interfaces. Like abstract classes, interfaces can’t be used for object creation. They must be inherited to derive a new class, which will provide the implementation for all the abstract methods of the interface and then objects be created using it.

Defining an Interface

An interface is defined much like a class. This is the general form of declaring an interface:

access interface name
{
return_type method_name1(parameter list);
return_type method_name2(parameter list);
type final_varname1 = value;
type final_varname2 = value;
…………………
…………………
return_type method_nameN(parameter list);
type final_varnameN = value;
}
Here, access is either public or private. The default scope is private, i.e., the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used anywhere even outside the package. Name is the name of the interface, and can be any valid identifier.
Interfaces are often declared public, because it makes them accessible to the widest range of code.  When an interface is declared public, it must be in a file having the same name of the interface.  In an interface, all the methods are abstract and public.  The method signature includes a return type, a name and zero or more number of parameters.
Variables can also be declared inside an interface. Variables of an interface are implicitly final and static, meaning they can’t be changed by the implementation class. They must also be initialized with a constant value. All methods and variables are implicitly public if the interface itself is declared as public.

Implementing an Interface

Once defined, one or more classes can implement an interface. And one class can implement any number of interfaces.  The relationship between interfaces and classes that implement them is many to many.  An interface can be implemented in one or more classes.  Similarly, one class can implement the methods of one or more interfaces.
To implement an interface, a class must provide the body (implementation) for all the methods defined in the interface.  When an interface is implemented by more than one class, each class is free to determine the details of its own implementation.
The following are the steps to follow to implement an interface in a class:
  1. Define a class that implements the interface using the keyword implements (not using extends keyword)
  2. With in the body of the class that implements, provide the code that implements the methods defined in the interface.
 The general form of a class that implements an interface is as follows:
class classname [extends superclass] implements interface
{
// body of the class
}
To implement more than one interface, the interface names are separated by comma in the list of interface names provided after the keyword implements.  Using the extends keyword allows the class being defined to extend the functionality of the superclass for implementing the interface.

Interface References

Since an interface is a type definition, it can be used to declare a reference variable of type interface.  This reference variable can be used to refer to an object that implements the interface.  When we call a method of an object through the inerface reference, it refers to the version of the method implemented by the object.  This process of accessing an object through interface reference is similar to using a super class reference to access a sub class object.  
Here  program is the algorithm and program in Java for implementing two interfaces in a single class:


Algorithm:
  1. Define two Interfaces named Square and Rectangle with method a single method computeArea(), which is overloaded with their various parameters.
  2. Implement both interfaces in a class named Area by providing code for computeArea() of both interfaces.
  3. Implement the method main() in another java file named MulInterface.java for creating the objects of Area and to compute the area for both square and rectangle.
  4. In method main(), declare the variables s, l and w of type float. Also create input object - din of type DataInputStream
  5. Declare a variable (objA) of type Area and create an object for it.
  6. Get the value of s from the user and call the method computeArea() of objA with s as the parameter to compute the area of square.
  7. Get the value of and w from the user and call the method computeArea() of objA with and w as parameters to compute the area of rectangle.
  8. Display the result – area of square and area of rectangle
Program in Java:
Area.java
interface Square
{
int sides = 3;
float computeArea(float side);
}


interface Rectangle
{
int sides = 4;
float computeArea(float length, float width);
}

public class Area implements Square, Rectangle
{
public float computeArea(float s)
{
return (s * s);
}
public float computeArea(float l, float w)
{
return (l * w);
}
}

MulInterface.java
import java.io.*;
public class MulInterface {


/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
float s, l, w;
DataInputStream din = new DataInputStream(System.in);
Area objA = new Area();
System.out.println("Enter the value of s (Side of a Square) : ");
s = Float.parseFloat(din.readLine());
System.out.println("The Area of the Square is : " + objA.computeArea(s));
System.out.println("Enter the value of l (Length of a Rectangle) : ");
l = Float.parseFloat(din.readLine());
System.out.println("Enter the value of w (Width of a Rectangle) : ");
w = Float.parseFloat(din.readLine());
System.out.println("The Area of the Rectangle is : " + objA.computeArea(l,w));
}
}

Sample Run:

Leave a Reply