Java Abstract Class – Master the Concept with its Rules & Coding Examples

We know that abstract class is the class that does not implement anything but is used as a template or a plan for other classes. Abstract classes are the most basic requirement to achieve Abstraction in Java.

In our previous article on Java Abstraction, we have already taken a brief look at Abstract Class in Java. Continuing the chain in this article, we are going to learn the concept of abstract classes in Java in detail with the help of coding examples.

But before that, it is recommended for you to take a quick revision on Java Abstraction to clear your basics with Techvidvan.

Java Abstract Class

An Abstract class is a class that represents a concept and whose objects can’t be created. A class that contains at least one abstract method (method without any implementation or method body) is called an abstract class.

An abstract class is declared with the help of an abstract keyword. There is always a default constructor in an abstract class, it can also have a parameterized constructor.

Note: Using an abstract class, we can achieve 0 to 100% abstraction.

Declaring a Java Abstract Class

To declare an abstract class in Java we use the keyword abstract. The syntax is given below:

abstract class ClassName
{
  //class body
}

Inheritance of Java Abstract Class

We can not create objects or instances from the abstract classes, but they can be subclassed. That is, to access the methods inside the abstract classes we have to inherit them. We will discuss the reason behind this concept in the later section of this article.

Revise the full concept of Java Inheritance in detail.

Code to illustrate the above concept:

//abstract parent class
abstract class Person
{
  public void displayInfo()
  {
    System.out.println("I am a person.");
  }
}
Inheritance of abstract class
class Student extends Person
{

}
class AbstractClassDemo
{
  public static void main(String[] args)
  {
    //Creating object of the child class
    Student obj1 = new Student();

    Accessing member of the abstract class
    obj1.displayInfo();
  }
}

Output:

I am a person

In the above example, we have created an abstract class Person. We cannot create objects of this abstract class Person and also cannot access the displayInfo() directly.

To access the displayInfo() method of Person, we have extended a subclass Student from the Person. Then we created the object obj1 of this subclass and used this object to access the method displayInfo().

Overriding Abstract Classes in Java

In Java, it is compulsory to override abstract methods of the parent class in its child class because the derived class extends the abstract methods of the base class.

If we do not override the abstract methods in the subclasses then there will be a compilation error. Therefore, it is necessary for a subclass to override the abstract methods of its base class.

Hold On! It’s the right time to get familiar with the concept of Method Overriding in Java. 

Note: If the subclass is also declared as abstract then it is not necessary to override the abstract methods.

Code to illustrate the above concept:

package com.techvidvan.abstractclass;
abstract class Parent
{
  //concrete method
  public void display1()
  {
    System.out.println("Concrete method of parent class");
  }
  //abstract method
  abstract public void display2();
}
class Child extends Parent
{
  // Must Override this method while extending Parent class
  public void display2()
  {
    System.out.println("Overriding abstract method");
  }

  //Overriding concrete method is not compulsory
  public void display1()
  {
    System.out.println("Overriding concrete method");
  }
}
public class AbstractClassDemo
{
  public static void main(String[] args)
  {
    Child obj = new Child();
    obj.display2();
    obj.display1();
  }
}

Output:

Overriding abstract method
Overriding concrete method

Why do we need Abstract Classes in Java?

You might be thinking what is the need for abstract classes if there is no implementation in them and also we can’t create an object from them.

To answer this question let’s take a situation where we want to create a class that just declares the structure or general form of a particular concept without providing a complete implementation of every method.

And, we want that this generalized form is shared by all of its child classes, and all the implementation details will be filled by these subclasses.

Let’s take the example of a banking application or software. Suppose we have a class BankAccount that has a method deposit() and withdraw() and the subclasses of it like SavingsAccount, CurrentAccount, FixedDepositAccount, etc.

Since the process of deposit and withdrawal differs from one account to another, there is no point to implement these two methods in the parent class BankAccount. This is because every child class must override these methods and provide an implementation of them.

Thus we can declare these methods as abstract in the parent class. That is, we will not provide any implementation of these abstract methods. Making this method abstract will enforce all the subclasses to implement these abstract methods otherwise, you will get a compilation error.

java abstract class example

Abstract Methods in Java

  • Abstract methods are methods with no implementation. They do not contain any method statement.
  • The child classes of this abstract class must provide the implementation of these inherited abstract methods.
  • An abstract method is declared with an abstract keyword.
  • The declaration of an abstract method must end with a semicolon ;

Get to know more about Java Methods in detail with Techvidvan.

Syntax of declaring abstract methods:

access-specifier abstract return-type method-name();

Example of Abstract class and Abstract methods:

package com.techvidvan.abstractclass;

//abstract parent class
abstract class BankAccount
{
  //abstract methods
  public abstract void deposit();
  public abstract void withdraw();

}
//SavingsAccount class extends BankAccount class
class SavingsAccount extends BankAccount
{
  public void deposit()
  {
    System.out.println("This is the concrete deposit method of SavingsAccount");
  }
  public void withdraw()
  {
    System.out.println("This is the concrete withdraw method of SavingsAccount\n");
  }
}
//CurrentAccount class extends BankAccount class
class CurrentAccount extends BankAccount
{
  public void deposit()
  {
    System.out.println("This is the concrete deposit method of CurrentAccount");
  }
  public void withdraw()
  {
    System.out.println("This is the concrete withdraw method of CurrentAccount\n");
  }
}
//FixedDepositAccount class extends BankAccount class
class FixedDepositAccount extends BankAccount
{
  public void deposit()
  {
    System.out.println("This is the concrete deposit method of FixedDepositAccount");
  }
  public void withdraw()
  {
    System.out.println("This is the concrete withdraw method of FixedDepositAccount");
  }
}
public class AbstractClassDemo
{
  public static void main(String args[])
  {
    BankAccount obj = new SavingsAccount();
    obj.deposit();
    obj.withdraw();

    BankAccount obj1 = new CurrentAccount();
    obj1.deposit();
    obj1.withdraw();

    BankAccount obj2 = new FixedDepositAccount();
    obj2.deposit();
    obj2.withdraw();
  }
}

Output:

This is the concrete deposit method of SavingsAccount
This is the concrete withdraw method of SavingsAccount
This is the concrete deposit method of CurrentAccount
This is the concrete withdraw method of CurrentAccount
This is the concrete deposit method of FixedDepositAccount
This is the concrete withdraw method of FixedDepositAccount

Why can’t we create an Object of an Abstract Class?

We can’t instantiate an abstract class because these classes are incomplete classes, with no implementation. Abstract classes have abstract methods that have no method body.

Suppose, if Java allows you to create an object of this class and use this object if someone calls the abstract method then what would happen? There would not be any actual implementation of the invoked method!

Also, an abstract class is like a general structure or a template that has to be extended by the subclasses for the implementation.

Code to illustrate that object creation of an abstract class is not valid:

As discussed above, we cannot instantiate an abstract class. This program throws a compilation error.

package com.techvidvan.abstractclass;
abstract class AbstractClass
{
  //Abstract method
  abstract public void display();
}
class MyClass extends AbstractionDemo
{
  public void display()
  {
    System.out.print("Abstract method");
  }
}
public class AbstractClassDemo
{
  public static void main(String args[])
  {
    //error: You can't create object of an abstract class
    AbstractClass obj = new AbstractClass();
    obj.display();
  }
}

Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type AbstractClass

Accessing Constructors of Abstract Class

As the constructors of non-abstract classes can be accessed, we can also access the constructor of an abstract class. We access the constructor from the subclass with the help of the super keyword. For example,

//parent class
abstract class Parent
{
  //constructor of the abstract class
  Parentl()
  {
    ….
  }
}
//child class
class Child extends Parent
{
  //constructor of the child class
  Child()
  {
    //Accessing the constructor of the abstract class using the super keyword
    super();
    ….
  }
}

To access the constructor of the parent class, we have used the super() inside the constructor of the Child. Note that the super statement should always be the first statement of the constructor of the subclass.

Rules for using Abstract Class in Java

Java Abstract Class Rules

There are some rules that you should remember while working the abstract classes.

  1. We can declare an abstract class using the abstract keyword.
  2. It may have abstract as well as concrete (non-abstract) methods.
  3. An abstract class can have static methods.
  4. An abstract class can also have constructors.
  5. It can have final methods. If we declare the method as final inside the abstract class then the subclass can not change the body of the method.
  6. We can’t instantiate or create an object of an abstract class.
  7. A class derived from the abstract parent class must implement each method that is declared as abstract in the parent class. Otherwise, there will be a compilation error.
  8. If the derived class does not implement all the abstract methods of an abstract parent class, then the derived class must also declare itself as abstract.

Using the final keyword in an Abstract Class

We can use the final keyword for variables, methods, and classes. The idea of using the final keyword is the same that is to provide security to the data, but its meaning changes from context to context.

  • When a final keyword is used with class, then the class can not be extended.
  • When we declare a variable as final, then it becomes a constant and its value can not be changed.
  • A method declared as final can not be overridden in the subclasses.

Note: An Abstract class may have final methods but an abstract class cannot be declared as final, otherwise we will not be able to extend it. And without extending an abstract class there is no use of it.!!

Code to illustrate the final keyword inside an abstract class:

package com.techvidvan.abstractclass;

//abstract parent class
abstract class Parent
{
  final int number = 10;
  public void display1() //if we declare it as final that it can not be overriden in the Child class
  {
    System.out.println("This is a non-abstract display method of the Parent Class");
  }
  abstract public void display2();
}
//concrete child class
class Child extends Parent
{
  @Override
  public void display1()
  {
    System.out.println("This is a non-abstract display method of the Child Class");
  }
  public void display2()
  {
    System.out.println("This is the implementation of an abstract display method of the Parent Class");
  }
}
public class AbstractClassDemo
{
  public static void main(String args[])
  {
    Parent obj = new Child();
    obj.display1();
    obj.display2();

    //Changing the value of the final variable will produce an error
    obj.number=15;
  }
}

Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The final field Parent.number cannot be assigned at project1/com.techvidvan.abstractclass.AbstractClassDemo.main(AbstractClassDemo.java:34)

To remove the error:

Comment the line:

obj.number=15;

After commenting on the above line, the output will be:

This is a non-abstract display method of the Child Class
This is the implementation of an abstract display method of the Parent Class

Difference between Abstract Class and Concrete Class

A concrete class is a non-abstract class that has an implementation of each method. That is, there are no abstract methods in the concrete class. An abstract class is useless until another class inherits it.

Let’s see the differences between concrete classes in an abstract class.

  1. If we declare any method as abstract in a class, then we also have to declare the class as abstract. An abstract class can also have non-abstract methods. On the other hand, a concrete class cannot have any abstract method. It has only non-abstract methods in it.
  2. To declare a class as abstract, we use the keyword abstract, while for declaring a concrete class we don’t use any keyword.
  3. We can create objects of concrete class but we can never instantiate an abstract class.
  4. To make use of an abstract class, we need to extend or inherit it, but it is not necessary to extend a concrete class to make it useful.
  5. We cannot declare an abstract class as final, but we can create a concrete class with the final keyword.

Summary

Abstract classes are declared with the abstract keywords and are used to achieve Abstraction in Java. Some important rules are to be kept in mind while working with an Abstract Class in Java.

Coming to the end of this article, we have learned the basic concept of Java Abstract Class along with its need, inheritance, methods, and rules of Abstract Class. We also explored the difference between Abstract Class and Concrete Class.

This article will surely help you in understanding the concept of Abstract Classes in Java along with their implementation and examples.

Thank you for reading our article. Do share your feedback through the Comment Section below.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.