INTERFACE
EXP-1: One interface implements into multiple classes(add and multiply) ,but the
method name is same(compute()).
//..........................Interface ..................//
interface calculate
{
public int compute(int a,int b);
}
//.......................... 1st Class Definition ..................//
class add implements calculate
{
public int compute(int a,int b)
{
return(a+b);
}
}
//.......................... 2nd Class Definition ..................//
class multiply implements calculate
{
public int compute(int a,int b)
{
return(a*b);
}
}
//.......................... Main Class Definition ..................//
class Multiple_Interface
{
public static void main(String args[])
{
add A=new add();
multiply M= new multiply();
calculate cal;
cal=(calculate) A;
System.out.println("Result:= "+ cal.compute(2,3));
cal=(calculate) M;
System.out.println("Result:= "+cal.compute(2,3));
}
}
Output:
Result:= 5
Result:= 6
1
EXP-2: One interface implements into multiple classes(Rectangle and Circle) ,but
the method name is same(compute_area()).
//..........................Interface ..................//
interface Area
{
final static float pi=3.14f;
float compute_area(float x,float y);
}
//.......................... 1st Class Definition ..................//
class Rectangle implements Area
{
public float compute_area(float x,float y)
{
return(x*y);
}
}
//.......................... 2nd Class Definition ..................//
class Circle implements Area
{
public float compute_area(float x,float y)
{
return(pi*x*x);
}
}
//.......................... Main Class Definition ..................//
class Interface_area
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area=rect;
System.out.println("Area of rectangle is="+area.compute_area(20,10));
area=cir;
System.out.println("Area of circle is="+area.compute_area(10,0));
}
}
Output:
Area of rectangle is=200.0
Area of circle is=314.0
2
EXP-3:Two interfaces(add and sub) are implemented into a single
class(expression).
//.......................... 1st Interface ..................//
interface add
{
public int sum(int a,int b);
}
//.......................... 2nd Interface ..................//
interface sub
{
public int minus(int a,int b);
}
//.......................... Implemented class Definition...................//
class expression implements add,sub
{
public int sum(int a,int b)
{
return(a+b);
}
public int minus(int a,int b)
{
return(a-b);
}
}
//.......................... Main class Definition...................//
class Multi_Interface_cal
{
public static void main(String args[])
{
expression exp=new expression();
add A=(add) exp;
sub S=(sub) exp;
int result=A.sum(10,20)*S.minus(15,5);
System.out.println("The Result of the expression is..."+result);
}
}
Output:
The Result of the expression is...300
3
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 and non-
Interface can have only abstract methods.
abstract methods.
2) Abstract class doesn't support multiple
Interface supports multiple inheritance.
inheritance.
3) Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.
4) Abstract class can have static methods, Interface can't have static methods, main
main method and constructor. method or constructor.
5) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
6) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
7) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves
fully abstraction (100%).
Example of abstract class and interface in Java
Let's see a simple example where we are using interface and abstract class both.
/……………………..Creating interface that has 4 methods ……………………………………………………./
interface A
{
void a();//bydefault, public and abstract
void b();
void c();
void d();
}
//Creating abstract class that provides the implementation of one method of A interface
abstract class B implements A
{
public void c(){System.out.println("I am C");}
4
}
//Creating subclass of abstract class, now provide the implementation of rest of the methods
class M extends B
{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
/………………………..Creating a test class that calls the methods of A interface ……………………………
…/
class Test5
{
public static void main(String args[])
{
A a=new M();
a.a();
a.b();
a.c();
a.d();
}
}
Output:
I am a
I am b
I am c
I am d