AppDividend
Latest Code Tutorials

Java Abstract Class Tutorial With Example | Abstract Class in Java

0

Java Abstract Class Tutorial With Example | Abstract Class in Java is today’s topic. In C++ programming language to make a class abstract we need to declare a pure virtual function in that class. But in Java, it’s different. To build a class virtual 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 Tutorial With Example

A class that is declared using the “abstract” keyword is known as an abstract class. It can have the abstract methods(methods without body) as well as concrete methods (conventional methods with a body). A standard class(non-abstract class) cannot have the abstract methods. In this guide, we will learn what an abstract class is, why we use abstract class, concrete class, and what are the rules that we must remember while working with abstract class in Java.

Why we need an abstract class

Let’s say we have a class called Document that has 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 to implement this method in the parent class. It is because every child class must override the format() method to give its implementation details like PDF class will output “pdf” content, and CSV class will output “csv.”

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

Abstract Method in Java

Now the question is what an abstract method is? In an abstract class, there can be a method which 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. See the below syntax of Java Abstract Method.

For now, let’s see some basics and example of an abstract method.

1) An abstract method has no body means it is a method without body statements.
2) Always end the method declaration with a semicolon(;).
3) It must be overridden. 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. Concrete class does not have any abstract method.

Syntax

abstract return_type  fuction_name();

There will be no further block of code.

How to declare an Abstract class in Java

Now learn how to declare an abstract class. See the below 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 it to keep in mind:

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

 

In that above example, it’s showing an error message is that we cannot be instantiated 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 bodies means its method without a body.

So if 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 concrete, an abstract class is like the template, so you have to extend it and build on it before you can use it or create an object.

That is why abstract class can not be instansiated.

If we want to resolve this problem, we need to declare another class which will have to extend the App class then we can access 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. Any class that has at least one abstract method then 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 method 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 which is not abstract is referred to as 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 Abstract class and PDF, CSV, and Word is Concrete class.

The concrete class implements the methods of Abstract class in Java. 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 class abstract we have to use keyword abstract. 1) To declare an interface, we need keyword interface
2) It can have both the abstract and non-abstract method. 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 member 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, 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 word problem.

See the below 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 below output.

Abstract Class in Java with example

 

Key Points of Java Abstract Class

  1. An abstract class has no use until unless some other class extends it.
  2. If you declare the abstract method in the class, then you must declare the class abstract as well. 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 then also it can be marked as abstract.
  3. It can have a non-abstract method (concrete) as well.
  4. It’s not necessary for the abstract class to have an abstract method. 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 is used to provide common method implementation to all the subclasses or to provide a default implementation.
  8. We can run an abstract class in java like any other class if it has a main() method.

Finally, the Java Abstract Class Tutorial With Example | Abstract Class in Java article is over.

Leave A Reply

Your email address will not be published.

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