The interface is a powerful tool. The same interface can be used by different classes for some method. Then this method can be implemented by each class in its own way.
Applying Interfaces
The
interface is a powerful tool. The same interface can be used by different
classes for some method. Then this method can be implemented by each class in
its own way. Thus same interface can provide variety of implementation. The
selection of different implementations is done at the run time. Following is a
simple Java program which illustrates this idea.
Step 1: Write a simple interface
as follows
Java Program [interface1.java]
interface
interface1
{
void
MyMsg(String s);
}
Step 2: Write a simple Java
Program having two classes having their own implementation of MyMsg method.
Java Program[Test.java]
import
java.io.*;
import
java.util.*;
class
Class1 implements interface1
{
private
String s;
public
void MyMsg(String s)
{
System.out.println("Hello
"+s);
}
}
class
Class2 implements interface1
{
private
String s;
public
void MyMsg(String s)
{
System.out.println("Hello
"+s);
}
}
class
Test
{
public
static void main(String[] args)
{
interface1
inter;
Class1
obj1=new Class1();
Class2
obj2=new Class2();
inter=obj1;
inter.MyMsg("User1");
inter=obj2;
inter.MyMsg("User2");
}
}
Step 3: Execute the program.
Program
Explanation:
• The
selection of method MyMsg is done at the runtime and it depends upon the object
which is assigned to the reference of the interface.
• We
have created reference inter for the interface in which the method MyMsg is
declared. Then the objects obj1 and obj2 are created for the classes class1 and
class2 respectively. When the obj1 is assigned to the reference inter then the
message "Hello User1" will be displayed because the string passed to
the method MyMsg is "User1". Similarly, when the obj2 is assigned to
the reference inter then the message "Hello User2" will be displayed
because the string passed to the method MyMsg is "User2".
• Thus
using the same interface different implementations can be selected.
Ex. 2.18.1: Write a program to
create interface method named customer. In this keep the methods called
information(), show() and also maintain the tax rate. Implement this interface
in employee class and calculate the tax of the employee based on their income.
Sol.:
Step 1: Create an interface file
named Customer.java. It is as follows -
interface
Customer
{
double
rate1=0.10;
double
rate2=0.20;
double
rate3=0.25;
double
income=0.0;
void
information();
void
show();
}
Step 2: Create a Java program
named Employee.java. It is as follows -
Employee.java
import
java.lang.System;
import
java.util.Scanner;
class
TestClass implements Customer
{
private
char sex;
private
double tax=0;
private
double netpay;
private
double income=0.0;
public
void information()
{
Scanner
in=new Scanner(System.in);
System.out.print("\n
Enter Sex(M or F)");
sex =
in.next().charAt(0);
System.out.println("\n
Enter your income");
income
in.nextDouble();
if(sex
== 'M')
{
if((income>=190000)&&(income<200000))
{
netpay
income;
}
else
if((income>=200000)&&(income<500000))
{
netpay
income-(income *rate1);
}
else
if((income>=500000)&&(income <1000000))
{
netpay
income-(income*rate2);
}
else
if(income>1000000)
netpay
income-(income*rate3);
}
else
{
if((income>=190000)&&(income<200000))
{
netpay
income;
}
else
if((income>=200000)&&(income<500000))
{
netpay
income;
}
else if((income>=500000)&&(income<1000000))
{
netpay
income-(income*rate1);
}
else
if(income>=1000000)
netpay-income-(income*rate2);
}
}
public
void show()
{
System.out.print("\n
The net Income of the person is\n" +netpay);
}
}
class
Employee
{
public
static void main(String[] args)
{
Customer
cust;
TestClass
obj = new TestClass();
cust=obj;
cust.information();
cust.show();
}
}
Step 3: Open the command prompt
and issue the following commands to execute the above program
D:\>javac
Employee.java
D:\>java
Employee
Enter
Sex(M or F)F
Enter
your income
500000
The net
Income of the person is 450000.0
Ex. 2.18.2: Define an interface
using JAVA that contains a method to calculate the perimeter of object. Define
two classes - circle and rectangle with suitable fields and methods. Implement
the interface "perimeter" in these classes. Write the appropriate
main() method to create object of each class and test all methods.
Sol.:
Step 1: We will write the simple
interface. The name of this interface is perimeter. In this interface the
method calculate is declared.
perimeter.java
interface
perimeter
{
void
calculate();
}
Step 2: Following is a java
program in which two classes are defined- circle and rectangle. The perimeter
for each of this class is calculated using the function declared in the
interface.
Main.java
import
java.io.*;
import
java.util.*;
import
java.util.Scanner;
class
circle implements perimeter
{
private
double r;
public
void calculate()
{
System.out.print("Enter
radius:");
Scanner
input=new Scanner(System.in);
double
r=input.nextDouble();
double
p=2*3.14*r;
System.out.println("Perimeter
of Circle: "+p);
}
}
class
rectangle implements perimeter
{
private
double length, breadth;
public
void calculate()
{
System.out.print("Enter
length:");
Scanner
input=new Scanner(System.in);
double
length=input.nextDouble();
System.out.print("Enter
breadth:");
double
breadth=input.nextDouble();
double
p=2*(length+breadth);
System.out.println("Perimeter
of rectangle: "+p);
}
}
class
Main
{
public
static void main(String[] args)
{
perimeter
obj;
circle
obj1=new circle();
rectangle
obj2=new rectangle();
System.out.println("\n\tCalculating
Perimeter of Circle....\n");
obj=obj1;
obj.calculate();
System.out.println("\n\t
Calculating Perimeter of Rectangle....\n");
obj=obj2;
obj.calculate();
}
}
Step 3: The output for this
program will be as follows –
Ex. 2.18.3 Write an interface
called shape with methods draw() and getArea(). Further design two classes
called Circle and Rectangle that implements Shape to compute area of respective
shapes. Use appropriate getter and setter methods. Write a Java program for the
same.
Sol. :
interface
Shape
{
void
draw();
double
getArea();
}
class
Circle implements Shape
{
private
double radius;
public
void setRadius(double r)
{
radius=r;
}
public
double getRadius()
{
return
radius;
}
public
double getArea()
{
return
(3.14* radius*radius);
}
public
void draw()
{
System.out.println("Area
of Circle is :"+getArea());
}
}
class
Rectangle implements Shape
{
private
double length, breadth;
public
void setLength(double 1)
{
length=1;
}
public
double getLength()
{
return
length;
}
public
void setBreadth (double b)
{
breadth=b;
}
public
double getBreadth()
{
return
breadth;
}
public
double getArea()
{
return
(length*breadth);
}
public
void draw()
{
System.out.println("Area
of Rectangle is :"+getArea());
}
}
class
Test
{
public
static void main(String[] args)
{
Circle
c=new Circle();
c.setRadius(10);
System.out.println("The
Radius of Circle is "+c.getRadius());
c.draw();
Rectangle
r=new Rectangle();
r.setLength(10);
r.setBreadth(20);
System.out.println("Length=
"+r.getLength()+", Breadth = "+r.getBreadth());
r.draw();
}
Output
The
Radius of Circle is 10.0
Area of
Circle is :314.0
Length =
10.0, Breadth = 20.0
Area of
Rectangle is :200.0
Nested
interface is an interface which is declared within another interface. For
example -
interface
MyInterface1 {
interface
MyInterface2 {
void
show();
}
}
class
NestedInterfaceDemo implements MyInterface1.MyInterface2 {
public
void show() {
System.out.println("Inside
Nested interface method");
}
public
static void main(String args[]) {
MyInterface1.MyInterface2
obj = new NestedInterfaceDemo();
obj.show();
}
}
Output
Inside
Nested interface method
Ex. 2.18.4 The transport
interface declares a deliver() method. The abstract class Animal is the super
class of Tiger, Camel, Deer and Donkey classes. The transport interface is
implemented by the Camel and Donkey classes. Write a test program to initialize
an array of four animal objects. If the object implements the transport
interface, the deliver() method is invoked.
Sol. :
interface
Transport
{
public
void deliver();
}
abstract
class Animal
{
String
item;
abstract
void deliver();
}
class
Tiger extends Animal
{
public
void deliver(){
}
}
class
Camel extends Animal implements Transport
{
Camel(String
s)
{
item=s;
}
public
void deliver(){
System.out.println("\tCamels
are used in deserts to carry "+item+".");
}
}
class
Deer extends Animal
{
public
void deliver(){
}
}
class
Donkey extends Animal implements Transport
{
Donkey(String
s) {
item=s;
}
public
void deliver(){
System.out.println("\tDonkeys
can carry "+item+".");
}
}
class
testclass
{
public
static void main(String args[]) throws NullPointerException
{
Transport[]
t=new Transport[4];
t[1]=new
Camel("goods and people");
t[2]=new
Donkey("heavy load");
System.out.println("Transport
interface is implemented by following animals...");
t[1].deliver();
t[2].deliver();
}
}
Output
Transport
interface is implemented by following animal...
Camels
are used in deserts to carry goods and people.
Donkeys
can carry heavy load.
Review
Question
1.
Explain simple interfaces and nested interfaces with examples.
Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : with Example Java Programs - Applying Interfaces
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation