You are on page 1of 7

Interface

An interface in java is a blueprint of a class.

It has static constants and abstract methods.

The interface is a mechanism to achieve abstraction.

There can be only abstract methods in the java interface not method body.

It is used to achieve abstraction and multiple inheritance in Java.

Java Interface also represents IS-A relationship.

It cannot be instantiated just like abstract class.

Three reasons to use interface

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

A class extends another class, an interface extends another interface but a class
implements an interface.
Example

Printable interface has only one method, its implementation is provided in the
A class.

interface printable
{
void printABC();
}

class ABC implements printable


{
public void print()
{
System.out.println("Hello");
printABC();
}

public static void main(String args[])


{
ABC obj = new ABC( );
obj.print();
PRINTABC();
}
}

Example: Drawable

Drawable interface has only one method.

 Its implementation is provided by Rectangle and Circle classes.


 In real scenario, interface is defined by someone
 But implementation is provided by different implementation providers.
 And, it is used by someone else.
 The implementation part is hidden by the user which uses the interface.

TestInterface1.java

//Interface declaration: by first user

interface Drawable // from US iam importing 3 rd party library


{
void draw( );
}
//WebDriver
//Implementation: by second user

class Rectangle implements Drawable


{
public void draw( )
{
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable
{
public void draw( )
{
System.out.println("drawing circle");
}
}

//Using interface: by third user


class TestInterface1
{
public static void main(String args[ ])
{
Drawable d=new Circle();
//Drawable d = new Rectangle();

WebDriver d; //driver =d
d.get( )
//In real scenario, object is provided by method e.g. getDrawable()
d.draw( );
}
}

Example: Bank

provides the implementation of Bank interface.

TestInterface2.java

interface Bank
{
float rateOfInterest( );

}
class SBI implements Bank
{

public float rateOfInterest( )


{
return 9.15f;
}
}
class AB implements Bank
{
public float rateOfInterest( )
{
return 9.7f;
}
}
class Test
{
public static void main(String[ ] args)
{
Bank bObject=new SBI( );

System.out.println("ROI: "+bObject.rateOfInterest());
}
}
Multiple inheritance by interface
If a class implements multiple interfaces, or an interface extends multiple
interfaces i.e. known as multiple inheritance.

interface Printable
{
void print();
}

interface Showable
{
void show();
}

class Abc implements Printable, Showable


{
public void print()
{
System.out.println("Hello");
}

public void show()


{
System.out.println("Welcome");
}

public static void main(String args[ ])


{
Abc obj = new Abc();
obj.print();
obj.show();
}
}

import selenium.WebDriver;

Class selenium
{
public static void main(String args[])
{
WebDriver driver = new ieDriver( );

}
Extends – class to class
Interface ---package –classes---- import

You might also like