An abstract method is a method prototype (i.e. return type, method name, list of parameters and optionally throws clause) without any implementation. Its implementation is provided by the subclass(es) of the class in which it is declared. To create an abstract method, simply specify the modifier abstract followed by the method declaration and replace the method body by a semicolon. For example, The abstract method area () of the Shape superclass will be written as
abstract double area();
The class that contains atleast one abstract method must be declared abstract. Such a class is known as an abstract class. A class is made abstract by putting the keyword abstract in front of class keyword in the first line of class definition. For example: The Shape superclass that contains abstract method area () will now look like,
abstract class Shape
{
abstract double area() ;//abstract method
abstract double circumference();
}
Abstract classes are like normal classes with fields and methods but you cannot create objects of abstract classes using the new operator. However, you can declare a variable of an abstract class type. Such variables are used to manipulate subclass objects polymorphically. In other words, such variable can be used to refer to an instance of any of the subclasses of abstract superclass.
For example: If you try to instantiate an object of the abstract superclass Shape using the following statement,
Shape s = new Shape(3,4); // error, Shape abstract
It will result in compile time error. Moreover, it makes no sense to create Shape object. What dimensions would it have? What would be its area?
Abstract classes are useful when you want to create a generic type that is used as a superclass for two or more subclasses, but the superclass itself does not represent an actual object. For example: As in case of Shape class which we need for inheritance and polymorphism but want only to instantiate objects of its subclasses, not Shape itself.
The following example demonstrates the use of abstract class.
abstract class Shape
{
abstract void area() ;
abstract void circumference();
}
class Rectangle extends Shape
{
private double length,breadth;
Rectangle(double x,double y)
{
length = x ;
breadth = y;
}
public void area()
{
System.out.println("Area of Rectangle is : " + (length*breadth));
}
public void circumference()
{
System.out.println("Circumference of Rectangle is : "+2*(length+breadth));
}
}
class Circle extends Shape
{
private double radius;
Circle(double r)
{
radius = r ;
}
public void area()
{
System.out.println("Area of Circle is : "+( Math.PI*radius*radius));
}
public void circumference()
{
System.out.println("Circumference of Circle is : "+ 2*Math.PI*radius);
}
}
class AbstractMethodsClasses
{
public static void main(String[] args)
{
Shape s; //Shape class reference variable
Rectangle r= new Rectangle(10,20);
s = r; //Assign rectangle reference to shape reference
s.area() ;
s.circumference() ;
Circle c = new Circle(5);
s = c; //Assign circle reference to shape reference
s.area();
s.circumference();
}
}