Sunday, May 11, 2008

Abstract Class and Interfaces

Abstract Classes and Interfaces allows you to create dynamic methods that act as per the object type generated at run time.

Abstract Class
  • Any class that contains one or more abstract methods needs to be declared as abstract.
  • There can be no objects for an abstract class
  • Abstract constructor or abstract static methods cannot be declared in a abstract class. You can construct regular constructor.
  • Any sub-class of an abstract class must implement all the abstract methods of the super calls or itself be declared abstract.

Interface

  • They lack instance variable, and their methods are declared without bodies.
  • Format

access interface name {

return-type method1 (param-list);
return-type method2 (param-list);
type final-var1=value;
type final-var2=value;

}

  • Each class that implements an interface must implement all of the methods defined under the interface.
  • Interface methods implemented by the class are always public.
  • A class that only implements an interface methods partially must be declared as an abstract class and all the rules of abstract class apply to it.

Example

abstract class Shape
{
//Static variable numObjects to keep count of Shape objects created
protected static int numObjects=0;

//Shape constructor
Shape()
{
++numObjects;
}
//define abstract method for area
abstract double area();

//static function to return number of shapes
protected static int numShapes()
{
return numObjects;
}

}

//Define interface for drawing
interface Drawable {
void draw();
}

//Class Circle is a sub class of Shape
class Circle extends Shape
{
protected int radius;
protected static int numberCircles=0;
Circle(int radius)
{
super(); //execute super class constructor first before initializing circle class instance variable - radius
this.radius=radius;
++numberCircles;
}

//return area of circle as double. This is the implementation for abstract method area defined in Shape
protected double area()
{
return Math.PI * radius * radius;
}

//static method to return number of circle objects created
protected static int numCircles()
{
return numberCircles;
}
}

//Class Rectangle is a sub class of Shape
class Rectangle extends Shape
{
protected int length,width;
protected static int numberRectangles=0;
Rectangle(int length, int width)
{
super(); //execute super class constructor first before initializing rectangle class instance variables length and width
this.length=length;
this.width=width;
++numberRectangles;
}

//return area of rectangle as double. This is the implementation for abstract method area defined in Shape
protected double area()
{
return length * width;
}

//static method to return number of rectangle objects created
protected static int numRectangles()
{
return numberRectangles;
}
}

//Graphic circle inherits circle class and implements interface Drawable
class GraphicCircle extends Circle
implements Drawable
{
protected int color;

GraphicCircle(int radius,int color)
{
super(radius);
this.color=color;

}
//interface implementation of draw method. method access should always be public for interface method implementation
public void draw()
{
System.out.println("Circle drawn using color:" + color);
}
}
//Graphic Rectangle inherits Rectangle class and implements interface Drawable
class GraphicRectangle extends Rectangle
implements Drawable
{
protected int color;


GraphicRectangle(int len,int wid,int color)
{
super(len,wid);
this.color=color;

}

//interface implementation of draw method. method access should always be public for interface method implementation
public void draw()
{
System.out.println("Rectangle drawn using color:" + color);
}
}

No comments: