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


Java Abstract Class Tutorial Example
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();
  }
}
Java Abstract Class Tutorial Example
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();
  }
}
Java Abstract Class Tutorial Example
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.

21 thoughts on “Java Abstract Class Tutorial Example”

  1. Dear Sir,

    I found this java is good for beginners. But Java pages are not filtered. Suppose if i click the next page, then different chapter will be shown and not according to chapter wise. i.e. after abstract classes chapter, different chapter is showing. I request you to arrange them according to chapter wise. Please let me know is this java notes available in PDF format? I really appreciate your effort and help!

    Thanks
    Rashmi

  2. abstract class Abstract {

    abstract void show();

    abstract void display();

    public void display2() {
    System.out.println(“display2 method”);
    }
    }

    public class subclass {

    void show() {
    System.out.println(“show method”);
    }

    void display() {
    System.out.println(“display method”);
    }

    public static void main(String[] args) {
    subclass a = new subclass();
    a.show();
    a.display();
    // a.display2();

    }

    }
    in this program with out extension of ‘Abstract’ class also ‘subclass’ class is implementing the ‘abstract’ class abstract methods how it is possible can plz guide me sir.

    1. Java treats show(), display() and display2() are the concrete methods of subclass (defined in subclass). These methods are in no way connected with class Abstract. Now don’t write show() in subclass, still your works (of course do not call show() from main()). Delete class Abstract, still your program works.

  3. abstract class Car
    {
    public abstract void fuel();
    public abstract void brake();
    public void aircon() // concrete method
    {
    System.out.println(“impelmenting concrete method in abstract class”);
    }
    public Car() // Constructor
    {
    System.out.println(“constructor of ABSTRACT class”);
    }
    public Car(String carname,int mileage)
    // overloading constructor with two parameters
    {
    System.out.println(“Car “+carname+” gives “+mileage);
    }

    }

    public class CarAbstract extends Car
    {
    public void fuel()
    {
    System.out.println(“implemened abstract method fuel”);
    }
    public void brake()
    {
    System.out.println(“implemened abstract method brake”);
    }
    public void aircon()
    {
    System.out.println(“overriding concretemethod of AB class in subclass”);
    super.aircon();

    }
    public static void main(String[] args)
    {
    CarAbstract c= new CarAbstract();
    c.fuel();
    c.brake();
    c.aircon();
    }

    }

    I created constructor overloading in Abstract class My doubt is how to access the constructor with two parameters.

    1. See this:

      abstract class Car
      {
      public abstract void fuel();
      public abstract void brake();
      public void aircon() // concrete method
      {
      System.out.println(“impelmenting concrete method in abstract class”);
      }
      public Car() // Constructor
      {
      System.out.println(“constructor of ABSTRACT class”);
      }
      public Car(String carname,int mileage)
      // overloading constructor with two parameters
      {
      System.out.println(“Car “+carname+” gives “+mileage);
      }

      }

      public class CarAbstract extends Car
      {
      public void fuel()
      {
      System.out.println(“implemened abstract method fuel”);
      }
      public void brake()
      {
      System.out.println(“implemened abstract method brake”);
      }
      public void aircon()
      {
      System.out.println(“overriding concretemethod of AB class in subclass”);
      super.aircon();

      }
      public CarAbstract()
      {
      super(“maruthi”, 15);
      }
      public static void main(String[] args)
      {
      CarAbstract c= new CarAbstract();
      c.fuel();
      c.brake();
      c.aircon();
      }

      }

  4. Sir, i have doubt. when we extends super class to sub class,we can use methods and variables of super class without creating an object of super class. Similarly, In case of abstract class you say that we declare super class as abstract (When no method is abstract ) because it just not allow to sub class to create an object of super class.

    so my question is, why we declare super class as abstract?

  5. if the abstract class have only concrete methods without having abstract methods then can we create class objects of abstract class ????

Leave a Comment

Your email address will not be published.