OOP – ASSIGNMENT IX

Lab Manual Download: Write a Java program which implements interface.

ASSIGNMENT NO 9

 Title: Implementation of Interface.

Objectives: To learn working of Java Interfaces.

Problem Statement:  Write a Java program which implements interface.

Outcomes: Students will be able to demonstrate use of interfaces in Java programs.

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

Software requirements:  64 bit Linux/Windows Operating System, JDK 1.4 or later

Theory:

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. 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
  • 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.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

  • It is used to achieve abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.

Understanding relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.

Declaring Interfaces:  The interface keyword is used to declare an interface. Here is a simple example to declare an interface −

Interfaces have the following properties −

  • An interface is implicitly abstract. You do not need to use theabstract 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.

Multiple inheritance in Java by interface: If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.

 Conclusion : In this assignment, we have studied interfaces in Java and demonstrated their use in program.

Program

interface Diagram
{
 double pi=(22/7);
 void putData();
 void getData();
 double area(); 
}
class Rectangle1 implements Diagram
{
 double width,height;
 public void putData()
 {
 System.out.println("Length and Height of Rectangle are:\t"+width+"\t"+height);
 System.out.println("Area of Rectangle "+area());
 
 }
 public void getData()
 {
 System.out.println("Enter length and height");
 width=Double.parseDouble(System.console().readLine());
 height=Double.parseDouble(System.console().readLine());
 }
 public double area()
 {
 return width*height;
 }
 
}
class Circle implements Diagram
{
 double radius;
 public void putData()
 {
 System.out.println("Radius of Circle:\t"+radius);
 System.out.println("Area of circle: \t "+area());
 System.out.println("Circumference of circle:\t "+circumference());
 }
 public void getData()
 {
  System.out.println("Enter radius");
 radius=Double.parseDouble(System.console().readLine());
 }
 public double area()
 {
 return pi*radius*radius;
 }
 public double circumference()
 {
 return (2* pi* radius);
 }
 
}
public class InterfaceDemo {
 public static void main(String args[])
 {
 Rectangle1 r=new Rectangle1();
 r.getData();
 r.putData();
 Circle c=new Circle();
 c.getData();
 c.putData();
 }
 
}

OUTPUT:

Leave a Reply

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