Java Tutorial - Java Abstract Class








Abstract class is for abstract idea or concept. For example, int data type is a concrete data type and double is another concrete data type. They are both numbers. Here number is an abstract concept. Shape is another example. We can have spare, rectangle or triangle or circle. They are all concrete while shape is an abstract class.

In Java we use abstract class to define the abstract concept. Abstract concept must have some abstract aspects. For example, the abstract concept is the Shape while the abstract aspect is how to calculate area. The abstract concept becomes abstract class in Java and the abstract aspect becomes the abstract method.





Syntax

You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. To declare an abstract method, use this general form:

abstract type name(parameter-list);

No method body is present for abstract method. Any class that contains one or more abstract methods must also be declared abstract.

abstract class MyAbstractClass{
   abstract type name(parameter-list); 
}

Here is an abstract class, followed by a class which implements its abstract method.

 
abstract class MyAbstractClass {
  abstract void callme();
/*from   w  ww . ja v  a 2  s  .co m*/
  void callmetoo() {
    System.out.println("This is a concrete method.");
  }
}

class B extends MyAbstractClass {
  void callme() {
    System.out.println("B's implementation of callme.");
  }
}

public class Main {
  public static void main(String args[]) {
    B b = new B();
    b.callme();
    b.callmetoo();
  }
}

The output:





Example

The following code defines Shape class as abstract. Shape class has abstract method called area(). Rectangle class extends abstract class Shape and implements the area() method for itself.

 
abstract class Shape {
  double height;/* www  . j a  va2  s.  co m*/
  double width;

  Shape(double a, double b) {
    height = a;
    width = b;
  }
  abstract double area();
}

class Rectangle extends Shape{
  Rectangle(double a, double b) {
    super(a, b);
  }
  double area() {
    System.out.println("Inside Area for Rectangle.");
    return height * width;
  }
}
class Triangle extends Shape{
  Triangle(double a, double b) {
    super(a, b);
  }
  double area() {
    System.out.println("Inside Area for Triangle.");
    return height * width / 2;

  }
}

public class Main {
  public static void main(String args[]) {
    Rectangle r = new Rectangle(10, 5);
    Triangle t = new Triangle(10, 8);

    Shape figref;

    figref = r;
    System.out.println("Area is " + figref.area());

    figref = t;
    System.out.println("Area is " + figref.area());
  }
}

The output: