9.8 Abstract Classes and Abstract Methods

polymorphismIn previous example we have created Rectangle and Triangle object. The Shape class only be used as super class for inheritance and polymorphism purpose not for object. The class that is not used for creating object is known as abstract.

To make a class abstract - put the keyword abstract before the class declaration :

abstract class Shape
{//code here}

Nobody can ever make a instance of abstract class. You can still use that abstract class as a declared refererence type for the purpose of polymorphism.

Abstract method

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract type name(parameter-list);

If a class includes abstract methods, then the class itself must be declared abstract. When an abstract method appears in a class, the method must be overridden in a subclass. If a subclass fails to override the method, an error will result. Abstract methods are used to ensure that a subclass implements the method.

Using an abstract class, you can improve the Shape class shown earlier. Since there is no meaningful concept of area for an undefined two-dimensional shape, the following version of the program declares getArea( ) as abstract inside Shape. This, of course, means that all classes derived from Shape must override getArea( ).

Program (Shape.java)

/**
 *  The Shape class is an abstract class that holds  
 *  general data about a shape.
 */

public abstract class Shape
{
   private double height;  // To hold height.
   private double width;  //To hold width or base

   // Set height and width
   public void setValues(double height, double width)
   {
      this.height = height;
      this.width = width;
   }

   //Get height
   public double getHeight() 
   {
       return height;
   }
     
   //Get width
   public double getWidth() 
   {
       return width;
   } 

   // The getArea method is abstract.   
   // It must be overridden in a subclass. 

   public abstract double getArea();
}

Program (Rectangle.java)

/**
 *  This class Rectangle calculates 
 *  the area of rectangle 
 */

public class Rectangle extends Shape
{
    //Calculate and return area of rectangle
    public double getArea()
    {
        return getHeight() * getWidth();
    }
}

Program (Triangle.java)

/**
 *  This class Triangle calculates 
 *  the area of triangle 
 */

public class Triangle extends Shape
{ 
    //Calculate and return area of triangle
    public double getArea() 
    {
        return (getHeight() * getWidth()) / 2;
    }
}       

Program(AbstractDemo.java)

/**
 * This program demonstrates polymorphic behavior.
 */

public class AbstractDemo
{
    public static void main(String[] args)
    {
        Shape shape;
        
        // assign subclass reference to subclass variable
        Rectangle rect = new Rectangle();
        
        // shape points to the object rect.
        shape = rect;

        // Set data in Rectangle's object 
        shape.setValues(78, 5);
        
        //Display the area of rectangle
        System.out.println("Area of rectangle : " + shape.getArea());
        
        // assign subclass reference to subclass variable
        Triangle tri = new Triangle();
        
        // shape points to the object rect.        
        shape = tri;
        
        // Set data in Triangle's object         
        shape.setValues(34,3);
        
        //Display the area of triangle        
        System.out.println("Area of triangle : " + shape.getArea());
    }
}

Output :
Area of rectangle : 390.0
Area of triangle: 51.0