English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java Abstract Classes and Abstract Methods

In this tutorial, we will learn about abstraction in Java. We will learn about Java abstract classes and methods, as well as how to use them in programs.

Java Abstract Class

An abstract class is a class that cannot be instantiated (we cannot create objects of an abstract class). In Java, we use the abstract keyword to declare an abstract class.

abstract class Animal {
   //Properties and methods
}

If an attempt is made to create an object of an abstract class, a compilation error will occur. For example,

Animal a1 = new Animal()

It will produce a compilation error:

 Animal is abstract; cannot be instantiated

Although we cannot instantiate abstract classes, we can create subclasses from them. We can create objects of the subclass to access the members of the abstract class.

Before understanding it in detail, we need to understand abstract methods.

Java abstract method

We use the same keyword abstract to create an abstract method. We declare an abstract method without implementing it. For example,

abstract void makeSound();

Here, makeSound() is an abstract method.

It is important to note that only one abstract class can contain abstract methods. If we include abstract methods in a class that is not abstract, an error will occur.

Abstract classes can contain abstract methods and non-abstract methods. This is an example.

abstract class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
   abstract void makeSound();
}

In the above example, we create an abstract class Animal. It contains an abstract method makeSound() and a non-abstract method displayInfo().

Inheritance of abstract class

Abstract classes cannot be instantiated. To access the members of an abstract class, we must inherit it. For example

Example1:Inheritance of abstract class

abstract class Animal {
 public void displayInfo() {
   System.out.println("I am an animal.");
 }
}
class Dog extends Animal {
}
class Main {
 public static void main(String[] args) {
   Dog d1 = new Dog();
   d1.displayInfo();
 }
}

Output:

I am an animal.

In the above example, we create an abstract class Animal. We cannot create an object of the Animal class. To access the displayInfo() method in the Animal class, we inherit the subclass Dog of Animal.

Then, we use Dog's d1The object accesses the displayInfo() method.

Overrides the abstract method

In Java, it is necessary to override the abstract methods of the superclass in the subclass. This is because the subclass inherits the abstract methods of the superclass.

 Because our subclass contains abstract methods, we need to override them.

Note:If the subclass is also declared as abstract, it is not necessary to force the rewriting of the abstract method.

Example2:Overrides the abstract method

abstract class Animal {
   abstract void makeSound();
   public void eat() {
      System.out.println("I can eat.");
   }
}
class Dog extends Animal {
   public void makeSound() {
      System.out.println("Bark bark");
   }
}
class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.makeSound();
      d1.eat();
   }
}

Output:

Bark bark.
I can eat.

In the above example, we create an abstract class Animal. This class contains an abstract method makeSound() and a non-abstract method eat().

We inherit a subclass Dog from the superclass Animal. Here, the subclass Dog overrides the abstract method displayInfo().

 Then we created the Dog object d1. Using the object, we called d1.displayInfo() and d1.eat() method.

Accessing the constructor of an abstract class

Similar to non-abstract classes, we use the super keyword to access the constructor of an abstract class from a subclass. For example,

abstract class Animal {
   Animal() {
      ….
   }
}
class Dog extends Animal {
   Dog() {
      super();
      ...
   }
}

Here, we use the super() in the Dog constructor to access the constructor of the Animal class.

Note that super should always be the first statement in the subclass constructor. AccessJava super keywordFor more information.

Why use Java abstraction?

Abstraction is an important concept in object-oriented programming. Abstraction only displays the required information, and all unnecessary details are kept hidden. This allows us to manage complexity by omitting or hiding details through the use of simpler, more advanced ideas.

An actual example of abstraction can be the motorcycle brake. We know the function of braking. When we press the brake, the motorcycle will stop. However, the working principle of the brake is hidden from us.

One of the main advantages of hiding the working principle of the brake is that now manufacturers can use different brakes for different motorcycles, but the function of the brake is the same.

Let's take an example to help us better understand Java abstraction.

Example3:Java abstract

abstract class Animal {
   abstract void makeSound();
}
class Dog extends Animal {
   public void makeSound() {
      System.out.println("Bark bark.");
   }
}
class Cat extends Animal {
   public void makeSound() {
      System.out.println("Meows ");
   }
}
class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.makeSound();
      Cat c1 = new Cat();
      c1.makeSound();
   }
}

Output:

Bark bark
Meows

In the above example, we created a superclass Animal. The superclass Animal has an abstract method makeSound().

The makeSound() method cannot be implemented within the Animal class. This is because each animal makes a different sound. Therefore, all subclasses of Animal have different makeSound() implementation methods.

We cannot implement makeSound() in Animal in a way that is correct for all subclasses of Animal. Therefore, the implementation of makeSound() in Animal remains hidden.

 In the above example, Dog implements its own makeSound() implementation, and Cat implements its own makeSound().

Points to Remember

  • We use the abstract keyword to create abstract classes and methods.

  • Abstract methods have no implementation (no method body).

  • The class that contains abstract methods should also be abstract.

  • We cannot create an object of an abstract class.

  • To implement the functionality of an abstract class, we inherit from its subclass and create an object of that subclass.

  • The subclass must override all abstract methods of the abstract class. However, if the subclass is declared as abstract, it is not mandatory to override the abstract methods.

  • We can use a reference to an abstract class to access the static properties and methods of an abstract class. For example,

    Animal.staticMethod();

Java Interface

In Java, interfaces are similar to abstract classes. However, interfaces have no non-abstract methods. In the next tutorial, we will learn more about interfaces.