Interface
The aim of interface is to dictate common behavior among objects from diverse classes.
Java does not allow multiple inheritance. To tie elements of different classes together Java uses an interface.
Interface body can only declare 2 things :
Abstract methods (empty) , must be public
Constants (public static final)
Eg :
interface employee
public static final double sal;
public void calsal();
Class can implement an interface using the keyword ‘implements’.
A class has to implement ALL the methods declared in the interface
Eg :
class manager implements employee
{
public void calsal()
{ sal = 50000; }
An interface can extend other interface.
A class can extend only one class but can implement as many interfaces.
ABSTRACT CLASSES
An abstract class is the one that simply represents a concept and whose objects can’t be created.
It is created through the use of keyword abstract
Abstract methods are methods with no method statements.
Keyword ‘abstract’ is used
Should be overridden
abstract class Shape
{ String name;
double area;
public abstract void disp(); // as disp() is abstract : must be overridden in derived class, // will not
have a body
}
class Circle extends Shape
{ double radius;
double calcArea()
{ radius=5.2;
return 3.15*radius*radius;
public void disp()// if not defined, will throw an error as abstract methods must be overridden
{ System.out.println("Radius = " + radius);
System.out.println("Area = " + calcArea());
} }
class Rectangle extends Shape
{ double length,breadth;
double calcArea()
{ length=9;
breadth=3;
return length*breadth;
}
public void disp()// if not defined, will throw an error as abstract methods must be overridden
{ System.out.println("Length = " + length);
System.out.println("Breadth = " + breadth);
System.out.println("Area = " + calcArea());
} }
public class abstractclass1
{ public void main()
{ //Shape s=new Shape(); // will give error, Shape is abstract
Circle c = new Circle();
c.calcArea();
c.disp();
Rectangle r = new Rectangle();
r.calcArea();
r.disp();
} }