Professional Documents
Culture Documents
Interface in Java: Reasons of Using Interface
Interface in Java: Reasons of Using Interface
But there are many differences between abstract class and interface that are given below.
1) Abstract class can have abstract 1) Interface can have only abstract methods.
and non-abstract methods.
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.
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%).
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
interface test1
{
void print();
}
class Atest implements test1
{
public void print()
{
System.out.println("Hello");
}
}
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[])
{
}
Multiple inheritance in java by interface:
interface A
{
void showA();
}
interface B
{
void showB();
}
}
Program-4 to demonstrate interface
interface A
{
void show();
}
interface B
{
void show();
}