Abstract Class in Java – Learn with its Important Rules & Example

Abstract Class in Java declares one or more abstract methods without the body. It is similar to the interface, the difference is that it can contain default method implementation. To create a class or method abstract we use the abstract keyword.

Note- We cannot instantiate an abstract class.

Before we start you should know the concept of Abstraction in Java.

Keeping you updated with latest technology trends, Join DataFlair on Telegram

1. Why we need an abstract class in Java?

For example, we have a class named Animal which has a method name sound() and the subclasses of this method are Cow, Cat, Dog, Horse, etc. The sounds of animals differ from one to another, so we don’t need to implement all the methods in the Animal class. The reason behind this is that every child class must override this method to give the implementation details, like the Cat will make the sound “Meow” and the Cow will say “Moo”.

So when we all the child of Animal class will override this method, there is no point to implement the method in the Animal class. Thus by making the method abstract will be a good choice. By making this method abstract we restrict all the child classes to implement this method and also we don’t need to implement this method in the parent class.

You must know the 4 different types of Inner Class in Java

Example of Abstract Class in Java

Java Abstract Class Example-

package com.dataflair.abstractclass;
//abstract parent class
abstract class Animal
{
      //abstract method
      public abstract void sound();
}

//Dog class extends Animal class
class Cat extends Animal
{
      public void sound(){
            System.out.println("Meow");
      }
}
public class AbstractClassExample
{
      public static void main(String args[]){
            Animal obj = new Cat();
            obj.sound();
      }
}

Example- 

Example of Abstract Class in Java

Important Points

  1. An abstract class may contain concrete methods.
  2. An abstract class can be declared without any abstract method.
  3. A class must be labelled as abstract if it has one or more abstract methods in Java.
  4. We cant instantiate an abstract class.
  5. If there is no implementation of an abstract class, then we use interface because in Java multiple class inheritance is no possible.
  6. Interface methods are implicitly abstract unless they are static or default.
  7. We can simply run an abstract class in java using the main() method.

Let’s Uncover the Difference Between Class and Interface in Java

2. Rules for Java Abstract Class

  1. We can declare an abstract class using the abstract keyword.
  2. We can have abstract and non-abstract methods in an abstract class.
  3. An abstract class can have constructors and static methods.
  4. An abstract class can have final methods which will force the subclass to not change the body of the method.
  5. We cant instantiate an abstract class.

Rules for Java Abstract Class

3. Abstract Class vs Concrete Class

A non-abstract class is a concrete class. An abstract class is of no use until it is extended by another class. By declaring a method as abstract then we need to declare the class abstract as well. We cannot have an abstract method in a concrete class and it cant have non-abstract methods.

  1. An abstract class is declared using abstract modifier while a concrete class doesn’t need abstract modifier.
  2. An abstract class cannot be instantiated while the concrete class can instantiate.
  3. An abstract class may contain abstract methods but a concrete class doesn’t have abstract methods.
  4. You need to inherit an abstract class to use it but it is mandatory to inherit a concrete class.
  5. An abstract class cannot be declared as final while a concrete class can be declared as final.
  6. In an abstract class implementation of an interface is not possible but in a concrete class implementing interface is possible.

Did you check what is Inheritance in Java?

Java Quiz

4. Summary

“Abstract” keyword is used to declare an abstract class in java. These classes are used to achieve the concept of abstraction. Some important rules are to be kept in mind while working with an abstract class in Java.

How was the explanation? Share your reviews through the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.