Menu
Topics Index
...
`


Abstract Class And Methods >
Siva Nookala - 15 Mar 2016
As discussed in Java Abstract Class, an abstract classes do not map to real life objects and they can not exist on their own, with out taking the form any of its concrete sub-classes. e.g., Shape can not exist on it own, it has to be either Rectangle or Square or Circle.

Abstract methods are those methods which have only the declaration but do not have a definition. Declaration means creating only the method signature (the method name, parameters and return type), but no method body, where as definition means creating the method signature and the method body as well.
For e.g., we know that every Shape has an area, but we do not know how to calculate the area, until we know what Shape it is. This is because the area calculation for Rectangle is different from the area calculation of Triangle, which is different from that of the Circle. So we will declare that we have a method called getArea() in Shape, but only define or implement the logic of calculating the area in there respective concrete sub-classes.
Calculate Areas
class CalculateAreas
{
    public static void main(String arg[])
    {
        Rectangle rect = new Rectangle(5.25, 4.0);
        System.out.println("Area of rectangle is " + rect.getArea());
        
        Circle circle = new Circle(7.5);
        System.out.println("Area of circle is " + circle.getArea());    
    }
}

abstract class Shape
{
    abstract double getArea(); // LINE A
}

class Rectangle extends Shape
{
    double length;
    double breadth;

    Rectangle(double length, double breadth)
    {
        this.length = length;
        this.breadth = breadth;
    }

    double getArea()
    {
        return length * breadth;
    }
}

class Circle extends Shape
{
    double radius;

    Circle(double radius)
    {
        this.radius = radius;
    }

    double getArea()
    {
        return 3.14 * radius * radius;
    }
}
OUTPUT

Area of rectangle is 21.0
Area of circle is 176.625

DESCRIPTION

Here we have created an abstract Shape class and two concrete classes namely Rectangle and Circle which extends from the Shape class. We have implemented the getArea() method in the sub-classes. Please note that every sub-class extending from an abstract class should implement all the abstract methods, otherwise it will cause compilation errors.

THINGS TO TRY
  • Define one more sub-class Triangle which extends from Shape class. Create a constructor, which takes the parameters base and height. Also implement the getArea() method. Area of triangle is (base * height) / 2.0
  • Add one more abstract method getPerimeter() in the Shape class and implement that method in Rectangle and Circle.
  • Define one more sub-class Polygon which extends from Shape. Do not define any constructors or methods in that class including the getArea() method. Observe the compilation error you get.
As shown in the above program at LINE A, the abstract method does not have any method body. The semicolon should be placed as soon as the method signature is complete. Please read Rules For Abstract Methods and Abstract Classes for more details.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.