Way2Java

Java Abstract Class Tutorial Example

Java Abstract Class Tutorial Example

Summary: By the end of this tutorial "Java Abstract Class Tutorial Example", you will be comfortable to practice abstract methods and abstract classes.

abstract Modifier

You have seen earlier two access modifiers – static and final. Now let us go for another modifier "abstract". "abstract" modifier (and also keyword) can be applied to methods and classes, but not with variables. "abstract" means no code or no implementation and just declares the method without the code.

We know earlier, super class is powerful and subclass is rich. Super class can impose many restrictions on the usage of its members by subclasses. One such you have seen earlier is final methods. Final methods of the super class cannot be overridden by the subclass. Now the super class would like a new restriction on the other way, just opposite. Super class would like to force the subclass to override (just, the opposite of final).

To achieve this, super class just declares its method without giving the body. As compiler does not accept the method without body, to satisfy the compiler, we declare the method as "abstract". That is, abstract method does not have body (no implementation). If the super class declares the method as abstract, subclass should and must override; else it is a compilation error. This is how a super class imposes a restriction on the subclass to forcibly override its method.

The compiler does not allow the super class to create an object as the class contains some methods without body. To satisfy the compiler, declare the class abstract. Declaring the class abstract, is a promise to the compiler that the programmer is not going to create an object of it. That is, with abstract classes, objects cannot be created.

Java Abstract Class Tutorial Example – Writing an Abstract Class

Let us see a program using abstract class.

abstract class Gandhiji
{
  public abstract void fatherOfNation();
  public abstract void independence();
}
public class GreatPeople extends Gandhiji
{
  public void fatherOfNation()
  {
    System.out.println("Gandhiji is the father of Indian nation");
  }
  public void independence()
  {
    System.out.println("Gandhiji's great principle, Ahimsa, got India, the independence");
  }
  public static void main(String args[])
  {
    GreatPeople gp1 = new GreatPeople();
    gp1.fatherOfNation();
    gp1.independence();
  }
}



Output screen of GreatPeople.java of Java Abstract Class Tutorial Example

In the above program, methods fatherOfNation() and independence() are declared as abstract as they do not contain body. Moreover, the Gandhiji class is declared as abstract as it contains abstract methods. These methods are overridden (given body) by the subclass GreatPeople. If GreatPeople does not override with implementation details, the program does not compile.

Super class gives the names and these names are used by the subclass with its choicest code. Let us make some rules as per the understanding of the program.

Rules in framing abstract classes
  1. Abstract method does not have body.
  2. If one of the methods of a class is abstract, the class should be declared as abstract.
  3. All the abstract methods of the super class should be overridden by the some class.
  4. With abstract classes, we cannot create objects (that is, abstract class cannot be instantiated).
Abstract Classes – Combinations

Abstract methods are designed to force the subclass to override. It is one of the design patterns. There are three variations of abstract classes.

  1. An abstract class having all abstract methods.
  2. An abstract class having a mixture of concrete and abstract methods.
  3. An abstract class having all concrete (non-abstract) methods.

Each combination has its own importance in coding design and the necessity is explained hereunder.

1. An abstract class having all abstract methods

In the earlier Gandhiji program all methods are abstract (includes two methods). All the methods are implemented with body by subclass, GreatPeople. If the programmer would like the subclass to give coding for all the methods, he declares all the methods as abstract.

2. Containing Abstract and Concrete Methods

Non-abstract methods are called as "concrete methods". The abstract class can contain a mixture of abstract and concrete methods. The architect chooses this style when the super class forces only a few of its methods to override and the other need not; then declares some methods as abstract and others not. This style permits the super class to give body (implementation) for some methods and some do not.

abstract class MotorCar
{
  public abstract void fuel();
  public abstract void brake();
  public void airConditioning()
  {
    System.out.println("Air conditioning is optional");
  }
}
public class Car extends MotorCar
{
  public void fuel()
  {
    System.out.println("Must to use fuel");
  }
  public void brake()
  {
    System.out.println("Must to have brakes");
  }
  public static void main(String args[])
  {
    Car c1 = new Car();
    c1.fuel();
    c1.brake();
    c1.airConditioning();
  }
}
Output screen of Car.java

MotorCar includes two abstract methods and one concrete class. The abstract methods, as per rule, are overridden by the subclass Car and the concrete method, airConditioning(), is used as it is. The concrete method is left to the discretion of the subclass to override or not.

3. Containing only Concrete Methods

Generally, many novices think that an abstract class should contain atleast one abstract method. But it is a wrong idea because an abstract class may contain all concrete methods and not even one abstract method. Why this style? It looks not necessary. Just go through the next program and notes.

abstract class MotorCar
{
  public void fuel()
  {
    System.out.println("Must to use fuel");
  }
  public void brake()
  {
    System.out.println("Must to have brakes");
  }
  public void airConditioning()
  {
    System.out.println("Air conditioning is optional");
  }
}
public class Car extends MotorCar
{
  public static void main(String args[])
  {
    Car c1 = new Car();
    c1.fuel();
    c1.brake();
    c1.airConditioning();
  }
}
Output screen of Car.java

The previous program is modified to suit the concept; an abstract class containing all concrete methods. All the methods, fuel(), brake() and airConditioning() are given body. Subclass can straight away use them.

Then, what is the necessity for the super class to declare abstract (when no method is abstract)? It just not to allow the subclass to create an object of super class. Subclass can make use of super class methods by composition.