Abstract Class in Java

Abstract classAbstraction

In the previous blog we learned about the final keyword in Java. If you want to visit the blog click Final Keyword in Java. In this blog, we will go through the Abstract class in Java. A class that has been declared with the keyword abstract is an Abstract class.

Abstraction in Java

Abstraction is nothing but the process of hiding the implementation details and showing only the essential details or functionality to the user. For example, if we take the example of a watch we are only concerned with the time, we are not concerned with how it is working internally.


Abstract Class in Java

Abstract class is nothing but a class that is declared abstract is called an Abstract class. An abstract class can contain abstract and non abstract methods. It must be extended and its method should be implemented in its subclass. Please make a note that:

  1. Abstract class must be declared as abstract, and it is not instantiated i.e. the object cannot be created of that class.
  2. Abstract class may or may not contain abstract methods.
  3. If a class has at least one abstract method, then the class must be declared abstract.
  4. If we want to declare a class as abstract, then we will have to inherit it from another class and also must provide implementation to the abstract methods in it.
  5. If we want to inherit an abstract class, then we will have to provide implementations to all the abstract methods in it.
...

The general syntax is as follows:

Syntax

abstract class <classname>{
  public abstract void abstractMethodName();
  public void normalMethodName(){
    ………           //method body
  }
}

As shown in the syntax, we have one abstract method and one non abstract method. We can see that the abstract keyword is used before class which means that it is an abstract class.


Abstract Methods

The method that is declared as abstract in its declaration without any implementation in the class is called the abstract method. An abstract method is declared inside the abstract class. When the class is declared as abstract, then that class is incomplete because we just declare that method with an abstract keyword. When we declare a method as an abstract, then that method is implemented in its child class i.e. a class that inherits the abstract class and then implements the abstract methods declared in the abstract class by overriding them. Due to which it becomes mandatory to override the abstract method in the child class. If the abstract method is not implemented in the child class as well, then we must declare the subclass also as “abstract”. We must remember one thing: we cannot write other keywords along with the abstract keyword. The syntax is as follows:

Syntax

abstract void methodName(parameterList);

Now we will go through the following examples:

  1. Example 1 - We will try to implement the class and will try to instantiate the object of the abstract class:
  2. abstract class Animal {
      abstract void sound();
        public void eat() {
          System.out.println("
    Inside the eat() method of Animal class.");
        }
      }
      class Cat extends Animal {
        public void sound() {
          System.out.println("Meow Meow");
        }
      }
    
      class AtrowelAbstractClassEx {
        public static void main(String[] args) {
          Animal objAnimal = new Animal();
          Cat objCat = new Cat();
          objCat.sound();
          objCat.eat();
        }
      }

    Output

    error: Animal is abstract; cannot be instantiated
      Animal objAnimal = new Animal();
                         ^
    1 error

    Here in the code it has given us an error because as we have seen in this blog that the abstract class cannot be instantiated and here in the AtrowelAbstractClassEx class, we are trying to create an instance of an Abstract class i.e. Animal which is not allowed. So the compiler has thrown an error saying “Animal is abstract; cannot be instantiated”.

  3. Example 2 - We will go through the example where we will define a class without abstract keyword but the call will contain the abstract method:
  4. class Animal {
      abstract void sound();
      public void eat() {
        System.out.println("Inside the eat() method of Animal class.");
      }
    }
    
    class Cat extends Animal {
      public void sound() {
        System.out.println("Meow Meow");
      }
    }
    
    class AtrowelAbstractClassEx {
      public static void main(String[] args) {
        Cat objCat = new Cat();
        objCat.sound();
        objCat.eat();
      }
    }

    Output

    Animal is not abstract and does not override abstract method sound() in Animal
      class Animal {
       ^
      1 error

    In the above code the class is implemented normally without adding the abstract keyword. But we can see that the same class has an abstract keyword because of which the compiler has given the error: ”Animal is not abstract and does not override abstract method sound() in Animal”, because when we define an abstract method in the class, then the class should be declared as abstract.

  5. Example 3 - Now we will declare the class as abstract along with the abstract method declared in it.
  6. abstract class Animal {
        abstract void sound();
        public void eat() {
          System.out.println("
    Inside the eat() method of Animal class.");
        }
      }
    
      class Cat extends Animal {
        public void sound() {
          System.out.println("Meow Meow");
        }
      }
    
      class AtrowelAbstractClassEx {
        public static void main(String[] args) {
          Cat objCat = new Cat();
          objCat.sound();
          objCat.eat();
        }
      }

    Output

    Meow Meow
    Inside the eat() method of Animal class.
  7. Example 4 - Abstract classes can also have final methods:
  8. abstract class BaseClassEx {
      final void display(){
        System.out.println("Inside the display() of BaseClassEx class");
      }
    }
    
    class DerivedxClassEx extends BaseClassEx {
    
    }
    
    class AtrowelAbstractClassEx {
      public static void main(String args[]){
        BaseClassEx atrowelObj = new DerivedxClassEx();
        atrowelObj.display();
      }
    }

    Output

    Inside the display() of BaseClassEx class

    Here in the above example we have defined the final method in the abstract class and that abstract class is derived by the DeriverClassEx class and we can also notice that in the class AtrowelAbstractClssEx class, we have created the object of the class DerivedClassEx, but the reference is of BaseClassEx which abstract and we have created the object of class DervivedClassEx which is assigned to the reference of the class BaseClassEx.

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.