Java Abstract Class: The Complete Guide

0
119
Java Abstract Class Tutorial With Example | Abstract Class in Java

Java abstract class that is declared using the “abstract” keyword is known as an abstract class. This is because, in C++ programming language, we need to declare a pure virtual function in that class to make a class abstract. But in Java, it’s different.

To build a virtual class, there should have at least one abstract method in that class. And to make any method abstract, there is a keyword “abstract”.

Java Abstract Class

A Java class that is declared using the “abstract” keyword is known as an abstract class. The abstract class can have abstract methods(methods without a body) and concrete methods (conventional methods with a body).

A standard class(non-abstract class) cannot have abstract methods. In this guide, we will learn what an abstract class is, why we use abstract and concrete classes, and the rules we must remember while working with an abstract class in Java.

Why we need an abstract class

Let’s say we have a document class with a method format(). Now its subclasses(see inheritance) are like PDF class, Word class, and CSV class.

Since the Document class differs from one format to another, there is no point in implementing this method in the parent class. It is because every child class must override the format() method to give its implementation details, like the PDF class will output “pdf” content, and the CSV class will output “csv”.

When we know that if all the Document’s child classes will and should override the format() method, then there is no point in implementing this method in the parent class. That is why making the format() method abstract would be a great choice, as by making this method abstract, we force all the subclasses to implement the format() method(otherwise, you will get a compilation error); also, we need not give any implementation to this method in the parent class.

Abstract Method in Java

What an abstract method is? In an abstract class, there can be a method that has no implementation in that class then the method is known as an abstract method. The declaration of an abstract method ends with a semicolon rather than a block. For example, see the below syntax of the Java Abstract Method.

Let’s now see some basics and examples of an abstract method.

1) An abstract method has nobody means. It is a method without body statements.
2) Always end the method declaration with a semicolon(;).
3) It must be overridden. For example, an abstract class must be extended, and in the same way, an abstract method must be overridden.
4) A class has to be declared as an abstract to have the abstract methods. For example, the concrete class does not have any abstract methods.

Syntax

abstract return_type  fuction_name();

There will be no other block of code.

How to declare an Abstract class in Java

To declare an abstract class in Java, use the abstract class class_name syntax.

Syntax

abstract class class_name           
{
   // abstract methods 
   // methods 
   // variables 
}

Now, if you are going to implement an abstract class, there are some essential points you need to keep in mind: First, an abstract class is the one whose instance cannot be created.

Let’s understand this with the help of an example.

// AbstractExample.java

//declaring abstract class
abstract class App {
  // decaring abstaract method
  abstract void welcome();

  // decalring normal method
  void great() {
    System.out.println("Hello guys, hope you all are doing well");
  }

}

// creating a normal class to access the abstract class
class Dividend extends App {
  // overriding the method of abstract class
  void welcome() {
    System.out.println("Welcome to AppDividend");
  }
}

class AbstractExample {
  public static void main(String[] er) {
     App app = new App();
  }
}

See the below output.

Java Abstract Class Tutorial With Example

The above example shows an error message that we cannot be instantiated the App class as it is an abstract class.

Why can’t we create an object of an abstract class

We can not create the object of an abstract class because the abstract class is incomplete and they have abstract methods that have no body means its method without a body.

So if the Java compiler allows us to create the object of the abstract class and if someone calls an abstract method using the object, then What would happen? There is no actual implementation of the abstract method to invoke.
Also, because an object is a concrete, an abstract class is like a template, so you have to extend it and build on it before you can use it or create an object.

That is why the abstract class can not be instantiated.

If we want to resolve this problem, we need to declare another class that will have to extend the App class then we can access it easily through the child class.

See the below code.

// AbstractExample.java

//declaring abstract class
abstract class App {
  // decaring abstaract method
  abstract void welcome();

  // decalring normal method
  void great() {
    System.out.println("Hello guys, hope you all are doing well");
  }

}

// creating a normal class to access the abstract class
class Dividend extends App {
  // overriding the method of abstract class
  void welcome() {
    System.out.println("Welcome to AppDividend");
  }
}

class AbstractExample {
  public static void main(String[] er) {
    /* App app=new App(); */
    // we cann't declare like this

    Dividend d = new Dividend();
    d.welcome();
    d.great();
  }
}

See the below output.

Abstract Class in Java

After correcting this output will be:

  1. For any class with at least one abstract method, it is compulsory to declare that class as an abstract class.
  2. An abstract class can contain both abstract and non-abstract methods.
  3. If a class extends an abstract class, then all the abstract methods should be overridden in the child class.
  4. It can have Constructor as well.

Now, see the below code.

// TestConstructor.java

//creating an abstract class
abstract class Hero {
  // constructor
  Hero() {
    System.out.println("Hero has been Created...");
  }

  // abstract method
  abstract void power();
}

class Ironman extends Hero {
  // overriding
  void power() {
    System.out.println("Power has been given to IronMan");
  }

  void fly() {
    System.out.println("Ironman is flying...");
  }
}

class TestConstructor {
  public static void main(String[] er) {
    Hero h = new Ironman();
    h.power();
    // h.fly();
  }
}

See the below Output.

Abstract Class in Java Tutorial

Difference between Abstract class & Concrete class

A class that is not abstract is referred to as a Concrete class. The concrete class has its properties and method implementation. If we take an example of Document and PDF, Word, and CSV classes, then Document is the Abstract class, and PDF, CSV, and Word is the Concrete class.

The concrete class implements the methods of the Abstract class in Java. Therefore, if you define the method abstract inside the Abstract class, then the concrete class which extends the abstract class must implement those methods.

Difference between Interface and Abstract class

See the below differences between Java Interface and Java abstract class.

               Abstract Class                     Interface
1) To make a class abstract, we must use the keyword abstract. 1) To declare an interface, we need a keyword interface
2) It can have both abstract and non-abstract methods. 2) By default, every method of Interface is an abstract method.
3) An abstract class extends with extends keyword. 3) An Interface extends with the implements keyword.
4) An abstract class can have a member like public, private, protected, etc. 4) All members in the Interface, by default public
5) It doesn’t support multiple inheritance. 5) It supports multiple inheritance.
6) It has static, non-static, final, and non-final variables. 6) It has only static and final variables.
7) Syntax:

      abstract class Hero {

           abstract void power ();

      }

7) Syntax:

interface Hero {

    void power ();

}

 

Now let’s see a real-world problem.

See the code.

// Auto.java

//abstract class
abstract class Vehicle {
  //// abstract method
  abstract void name();

  // non-abstract method
  int milage(int a) {
    return a;
  }
}

class Bike extends Vehicle {
  // overriding method
  void name() {
    System.out.println("Vehicle name: Bike");
  }
}

class Car extends Vehicle {
  // overriding method
  void name() {
    System.out.println("Vehicle name: Car");
  }

}

class Auto {
  public static void main(String[] er) {
    Bike b = new Bike();// creating instance for Bike
    Car c = new Car();// creating instance for Car
    b.name();
    System.out.println("Milage of Bike is " + b.milage(50) + "KMPL");
    c.name();
    System.out.println("Milage of Car is " + c.milage(25) + "KMPL");
  }
}

See the output.

Abstract Class in Java with example

Key Points of Java Abstract Class

  1. An abstract class has no use unless some other class extends it.
  2. If you declare the abstract method in the class, you must also declare the class abstract. You can’t have an abstract method in the concrete class. The vice versa is not always true: If the class does not have any abstract method, it can also be marked as abstract.
  3. It can have a non-abstract method (concrete) as well.
  4. The abstract class doesn’t need to have an abstract method. So we can mark the class as abstract even if it doesn’t declare any abstract methods.
  5. The subclass of abstract class in Java must implement all the abstract methods unless the subclass is also an abstract class.
  6. Java Abstract class can implement interfaces without even providing the implementation of interface methods.
  7. Java Abstract class provides joint method implementation to all the subclasses or a default implementation.
  8. We can run an abstract class in Java like any other class if it has a main() method.

That’s it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.