Java Abstract class

In this chapter you will learn:

  1. What is abstract class
  2. How to create abstract class
  3. Example - abstract class
  4. A demo for using abstract methods and classes

Description

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); 
}

Example

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

 
abstract class MyAbstractClass {
  abstract void callme();
/* w w w . jav  a 2s  .com*/
  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 2

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;// w  w  w . ja  va2 s .  c o  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:

Next chapter...

What you will learn in the next chapter:

  1. What is Java access control level
  2. Java Class Access Control Level
  3. Example - effects of public and private access
  4. How does the access control act on the inheritance
  5. What is the access matrix for Java
  6. What are the targets for different access modifiers