Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Interface in Java

 It is a blue print of class.


 It has static constants and abstract methods.
 It is used to achieve abstraction and multiple inheritance in java.
 We can say interfaces can have abstract methods and variables. It can not have a body.
 It can not be instantiated like abstract class.

Reasons of using interface


a) To achieve abstraction
b) Support for multiple inheritance
c) To achieve loose coupling

Similarities and difference between abstract class and interface


 Abstract class and interface both are used to achieve abstraction where we can declare the
abstract methods.
 Abstract class and interface both can't be instantiated.

But there are many differences between abstract class and interface that are given below.

Abstract class Interface

1) Abstract class can have abstract 1) Interface can have only abstract methods.
and non-abstract methods.

2) Abstract class doesn't support 2) Interface supports multiple inheritance.


multiple inheritance.

3) Abstract class can have final, 3) Interface has only static and final variables.
non-final, static and non-static
variables.

4) Abstract class can provide the 4) Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to 5) The interface keyword is used to declare interface.
declare abstract class.

6) An abstract class can extend 6) An interface can extend another Java interface only.
another Java class and implement
multiple Java interfaces.

7) An abstract class can be 7) An interface can be implemented using keyword "


extended using keyword "extends". implements".

8) A Java abstract class can have 8) Members of a Java interface are public by default.
class members like private,
protected, etc.

We can say that abstract class achieves partial abstraction (0 to 100%) whereas interface
achieves fully abstraction (100%).

Syntax fo interface declaration:

interface interface-name
{
Declare constant fields
Declare abstract methods
}

Note: The java compiler adds public and abstract keywords before the interface method.
Moreover, it adds public, static and final keywords before data members

Relationship between classes and interfaces:

a) A class extends another class


b) An interface extends another interface
c) But class implements an interface
Let us see examples of interface in Java:-

Program-1 to demonstrate interface

interface test1
{
void print();
}
class Atest implements test1
{
public void print()
{
System.out.println("Hello");
}

public static void main(String args[])


{

Atest obj=new Atest();


obj.print();

}
Program-2 to demonstrate interface

interface Banktest
{
float rateofinterest();
}
class PNB implements Banktest
{
public float rateofinterest()
{
return 9.25f;
}

}
class SBI implements Banktest
{
public float rateofinterest()
{
return 8.75f;
}

}
class HDFC implements Banktest
{
public float rateofinterest()
{
return 9.50f;
}

}
class Testinterface
{
public static void main(String args[])
{

Banktest obj=new PNB();


System.out.println(" Rate of interest = " + obj.rateofinterest());

}
Multiple inheritance in java by interface:

Program-3 to demonstrate interface

interface A
{
void showA();
}
interface B
{
void showB();
}

class ABtest implements A,B


{
public void showA()
{
System.out.println("ShowA");
}

public void showB()


{
System.out.println("ShowB");
}
public static void main(String args[])
{

ABtest obj=new ABtest();


obj.showA();
obj.showB();

}
Program-4 to demonstrate interface

interface A
{
void show();
}
interface B
{
void show();
}

class ABmultipleinheritance implements A,B


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

public static void main(String args[])


{

ABmultipleinheritance obj=new ABmultipleinheritance();


obj.show();

You might also like