Java Program Using User Defined Interface Concept

Aim:

To write a java program using user defined interface concept.

INTERFACES-DEVELOPING USER DEFINED INTERFACES

Interfaces in Java

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).  

  • Interfaces specify what a class must do and not how. It is the blueprint of the class.
  • An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
  • A Java library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection.

Syntax :

interface  {
// declare constant fields
// declare methods that abstract 
// by default.
}
  • To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword.

Why do we use interface ?

  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
  • It is also used to achieve loose coupling.
  • Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?

The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.

An interface is similar to a class in the following ways −

  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including −

  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

Interfaces have the following properties −

  • An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.

Implementing Interfaces

When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.

A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.

When overriding methods defined in interfaces, there are several rules to be followed −

  • Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
  • The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
  • An implementation class itself can be abstract and if so, interface methods need not be implemented.

When implementation interfaces, there are several rules −

  • A class can implement more than one interface at a time.
  • A class can extend only one class, but implement many interfaces.
  • An interface can extend another interface, in a similar way as a class can extend another class.

Extending Interfaces

An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

Extending Multiple Interfaces

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

Aim:

To write a java program using user defined interface concept.

Algorithm:

  1. Start the program
  2. Declare the interface called area and declare the function called compute in it.
  3. Define the class called rectangle and implement the interface area.
  4. Define the class called circle and implement the interface area.
  5. Display the result.

Program:

import java.util.Scanner;
interface Area{
void compute();
}
class Rectangle implements Area
{
private int lenght,breadth;
Rectangle(int a,int b)
{
this.lenght=a;
this.breadth=b;
}
public void compute()
{
int result= lenght*breadth;
System.out.println("AREA OF RECATANGLE ="+result);
}
}
class Circle implements Area
{
private int radius;
Circle (int a)
{
this.radius=a;
}
public void compute()
{
double result= 3.14*radius*radius;
System.out.println("AREA OF CIRCLE ="+result);
}
}
public class Main
{
 public static void main(String[] args) {
int a,b;
Scanner put=new Scanner(System.in);
System.out.println("ENTER RECATANGLE LENGTH AND BREATH ");
a=put.nextInt();
b=put.nextInt();
Rectangle A=new Rectangle(a,b);
System.out.println("ENTER RADIUS OF CIRCLE  ");
a=put.nextInt();
Circle B=new Circle(a);
A.compute();
B.compute();
}
}

Execution:

INPUT :

ENTER  RECTANGLE  LENGTH  AND  BREATH: 4 6
ENTER RADIUS OF CIRCLE: 8

OUTPUT : 
AREA OF RECTANGLE  = 24
AREA OF CIRCLE = 200.96

Leave a Comment