See the Tutorial List

Abstract class and methods in Java

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 in Java

If a class contain any abstract method then the class is declared as abstract class. An abstract class is never instantiated. It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method.

Syntax:

abstract class class_name { }

Abstract method in Java

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 declared by the super class.

Syntax:

abstract return_type function_name (); //No definition

Java: Example of Abstract class

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

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

this is callme.


Java: Abstract class with concrete(normal) method

Abstract classes can also have normal methods with definitions, along with abstract methods.

abstract class A
{
  abstract void callme();
  public void normal()
  {
    System.out.println("this is concrete method");
  }
}

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

this is callme. this is concrete method.


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.

Java: Abstraction using Abstract class

Abstraction is an important feature of OOPS. It means hiding complexity. 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.