Signup/Sign In

Java Abstract class and methods

In this tutorial, we will learn about abstract class and methods in Java along with understanding how we can implement abstraction using abstract classes. We will also have some code examples.

Abstract Class

A class which is declared using abstract keyword known as abstract class. An abstract class may or may not have abstract methods. We cannot create object of abstract class.

It is used to achieve abstraction but it does not provide 100% abstraction because it can have concrete methods.

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It is used for abstraction.

Syntax:

abstract class class_name { }

Abstract method

Method that are declared without any body within an abstract class are called abstract method. The method body will be defined by its subclass. Abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods.

Syntax:

abstract return_type function_name (); //No definition

Points to Remember about Abstract Class and Method

Here are some useful points to remember:

  1. Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.
  2. An abstract class may or may not have an abstract method. But if any class has even a single abstract method, then it must be declared abstract.
  3. Abstract classes can have Constructors, Member variables and Normal methods.
  4. Abstract classes are never instantiated.
  5. When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.

Example of Abstract class

Let's take an example of the Abstract class and try to understand how they can be used in Java.

In this example, we created an abstract class A that contains a method callme() and Using class B, we are extending the abstract class.

abstract class A
{
  abstract void callme();
}
class B extends A
{
  void callme()
  {
    System.out.println("Calling...");
  }
  public static void main(String[] args)
  {  
    B b = new B();
    b.callme();
  }
}

calling...

Abstract class with non-abstract method

Abstract classes can also have non abstract methods along with abstract methods. But remember while extending the class, provide definition for the abstract method.

abstract class A
{
  abstract void callme();
  public void show()
  {
    System.out.println("this is non-abstract method");
  }
}

class B extends A
{
  void callme()
  {
    System.out.println("Calling...");
  }
  public static void main(String[] args)
  {
    B b = new B();
    b.callme();
    b.show();
  }

calling... this is non-abstract method

Abstraction using Abstract class

Abstraction is an important feature of OOPS. It means hiding complexity and show functionality only to the user. Abstract class is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method. Lets see how abstract class is used to provide abstraction.

abstract class Vehicle
{
   public abstract void engine();
}
public class Car extends Vehicle {

    public void engine()
    {
        System.out.println("Car engine");
        // car engine implementation
    }

    public static void main(String[] args)
    {
        Vehicle v = new Car();
        v.engine();
    }
}

Car engine

Here by casting instance of Car type to Vehicle reference, we are hiding the complexity of Car type under Vehicle. Now the Vehicle reference can be used to provide the implementation but it will hide the actual implementation process.

When to use Abstract Methods & Abstract Class?

Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.

Abstract classes are used to define generic types of behaviors at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.