blog posts

Abstract Classes And Methods In Java

In this tutorial, you will learn about abstraction in Java and learn about abstract Java classes and methods and how to use them in the program.

Java abstract class

An abstract class is a class that cannot be introduced (we cannot create an object from an abstract class). In Java, we use the abstract keyword to define an abstract class.

  1. abstract class Animal {
  2. // attributes and methods
  3. }

If we try to create an object from the abstract class, the compiler gets an error. For example :

  1. Animal a1 = new Animal ()

Which causes a compiler error:

Animal is abstract; cannot be instantiated

Although abstract classes cannot be defined, we can create objects from subclasses and subclasses to access abstract class members.

Before we talk about them in detail, we need to understand abstract methods.

Abstract Java method

We use the same abstract keyword to create abstract methods. An abstract method is defined without execution. For example:

  1. abstract void makeSound ();

Here, makeSound () is an abstract method. Body of makeSound () with is already replaced.

It is important to note that only one abstract class can contain abstract methods. If we put abstract methods inside a class that is not abstract, we get an error.

An abstract class can include both abstract and non-abstract methods. Consider the following example:

  1. abstract class Animal {
  2. public void displayInfo () {
  3. System.out.println (“I am an animal.”);
  4. }
  5. abstract void makeSound ();
  6. }

In the example above, we created an abstract Animal class that includes an abstract makeSound () method and a non-abstract displayInfo () method.

Inheritance from the abstract class

The abstract class cannot be introduced. To access members of an abstract class, we must inherit from it.

Example 1: Inheritance from the abstract class

  1. abstract class Animal {
  2. public void displayInfo () {
  3. System.out.println (“I am an animal.”);
  4. }
  5. }
  6. class Dog extends Animal {
  7. }
  8. class Main {
  9. public static void main (String [] args) {
  10. Dog d1 = new Dog ();
  11. d1.displayInfo ();
  12. }
  13. }

Output

I am an animal.

In the example above, we created an abstract Animal class. But we can not create an object from Animal. To access displayInfo (), we created a Dog subclass of Animal (inherited from the Animal class)

Then we used the Dog d1 object to access the displayInfo () method.

Overriding (abstracting methods of inherited methods) Abstract methods

In Java, overriding superclass abstract methods is mandatory under the class. Because the subclass inherits the superclass abstract methods.

Since the subclass contains abstract methods, we must re-implement them.

Note: Overriding is not mandatory if abstract subclasses are also defined.

Example 2: Overriding an abstract method

  1. abstract class Animal {
  2. abstract void makeSound ();
  3. public void eat () {
  4. System.out.println (“I can eat.”);
  5. }
  6. }
  7. class Dog extends Animal {
  8. public void makeSound () {
  9. System.out.println (“Bark bark”);
  10. }
  11. }
  12. class Main {
  13. public static void main (String [] args) {
  14. Dog d1 = new Dog ();
  15. d1.makeSound ();
  16. d1.eat ();
  17. }
  18. }

Output

Bark bark.

I can eat.

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

The Dog subclass inherits from the Animal superclass. Here, the Dog subclass re-implements the displayInfo () abstract method.

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

Access the abstract class builder

Similar to non-abstract classes, we use super key to access the constructor of an abstract class below the class. For example,

  1. abstract class Animal {
  2. Animal () {
  3. ….
  4. }
  5. }
  6. class Dog extends Animal {
  7. Dog () {
  8. super ();
  9. }
  10. }

Here, we used super () in the Dog constructor to access the Animal constructor.

Note that super should always be the first constructive sentence of the subclass.

Why Java Abstract?

Abstract is an important concept of object-oriented programming. The abstraction shows only the required information and all unnecessary details are hidden. This allows us to manage complexity by removing or hiding details with a simpler, higher-level idea.

A practical example of abstraction is motorcycle brakes. We know what the brake does. When we brake, the engine stops. However, the work of the brakes is kept secret from us.

The most important advantage of hiding brake work is that the manufacturer can now apply the brake differently for different engines, however, what the brake does is the same.

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

Example 3: Java abstraction

  1. abstract class Animal {
  2. abstract void makeSound ();
  3. }
  4. class Dog extends Animal {
  5. public void makeSound () {
  6. System.out.println (“Bark bark.”);
  7. }
  8. }
  9. class Cat extends Animal {
  10. public void makeSound () {
  11. System.out.println (“Meows”);
  12. }
  13. }
  14. class Main {
  15. public static void main (String [] args) {
  16. Dog d1 = new Dog ();
  17. d1.makeSound ();
  18. Cat c1 = new Cat ();
  19. c1.makeSound ();
  20. }
  21. }

Output

Bark bark

Meows

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

The makeSound () method is not executable in Animal. Because each animal makes different sounds. Therefore, all Animal subclasses have a different execution of makeSound ().

We cannot execute makeSound () in Animal in such a way that it can be true for all its subclasses. Therefore, the makeSound () execution in Animal is kept secret.

In the example above, Dog executes from makeSound () and Cat performs its own execution from makeSound ().

Key points to remember

  • We use the abstract keyword to create abstract classes and methods.
  • An abstract method has no implementation (method body).
  • The class containing abstract methods must also be abstract.
  • We can not create objects from the abstract class.
  • To implement the properties of an abstract class, subclasses inherit from it and create an object from the subclass.
  • A subclass must ignore (re-implement) all abstract methods of an abstract class. But if an abstract subclass is defined, ignoring abstract methods is not mandatory.
  • We can access the static properties and methods of an abstract class using the abstract class reference. For example :
  1. Animal.staticMethod ();