Java - Abstract Class

Java

Share

In this tutorial we will learn about abstract class in Java programming language.

In the previous tutorials we learned about Inheritance and Method Overriding. Feel free to check that out.

So, using method overriding a subclass or child class overrides a method of the superclass or parent class by creating method that shares the same name as the method in the parent class.

Abstraction

Abstraction is a concept in which a parent class or superclass has a method that is not implemented. That is, a method without a body. And it is expected that the child class or subclass inheriting the parent class will implement the method.

We use the abstract keyword to mark an abstract class.

The method that needs to be implemented by the child class is also marked as abstract.

Syntax

abstract class MyClass {
  
  abstract type myMethod(param_list);

  // some code goes here...

}

Where, MyClass is the name of the abstract class as it is marked by the abstract keyword.

The method named myMethod is expected to be implemented by the child class is marked abstract.

The return type of the abstract method is represented by type and param_list denotes the list of parameters that the abstract method will have.

Points to note!

Following are the points to note when using abstraction.

  • Abstract class must have the abstract keyword before the class keyword.
  • Methods that will be implemented by the child class must be marked with the abstract keyword.
  • If the child class inheriting the abstract class does not implements the abstract method then the child class must also be marked as abstract class.
  • Abstract class can also have fully implemented methods and other member variables.
  • We can't instantiate an object of an abstract class.

Example #1: Inheriting an abstract class

In the following example we have an abstract class MyMessage with an abstract method greetings.

This class is inherited by the HelloWorld class and inside it the abstract method greetings is implemented.

// abstract class
abstract class MyMessage {
  
  // member variable
  String message;

  // abstract method that needs
  // to be implemented by child class
  abstract void greetings();

  // fully implemented method
  public void awesome() {
    System.out.println("Awesome!");
  }
}

// class inheriting the abstract class
class HelloWorld extends MyMessage {

  // constructor
  HelloWorld(String message) {

    // note!
    // this message is inherited from
    // the parent class MyMessage
    this.message = message;
  }

  // implementing the abstract method
  // of the parent class
  void greetings() {
    System.out.println("Hello World");
  }

  public void showMessage() {
    System.out.println("Message: " + this.message);
  }
}

// main class
public class Example {
  public static void main(String[] args) {

    // instantiate an object
    HelloWorld obj = new HelloWorld("My name is Yusuf Shakeel.");

    // method call
    obj.greetings();

    obj.awesome();

    obj.showMessage();

  }
}

Output:

$ javac Example.java 
$ java Example
Hello World
Awesome!
Message: My name is Yusuf Shakeel.

Example #2: Multilevel inheritance

In the following example we have Happy abstract class that has an abstract method greetings.

The Happy class is inherited by the Awesome class but the abstract method is not implemented here. So, we mark the Awesome class as abstract.

Then we have the HelloWorld class that inherits the Awesome class and implements the abstract greetings method.

// abstract class
abstract class Happy {
  
  // abstract method that needs
  // to be implemented by child class
  abstract void greetings();
}

// inheriting the abstract class
// but not implementing the abstract method
// so, this class is marked as abstract
abstract class Awesome extends Happy {
  
  // abstract method that was inherited from
  // the parent class Happy but not
  // implemented in this class
  // so marked as abstract method
  abstract void greetings();
}

// this class implements the abstract method
class HelloWorld extends Awesome {

  // implementing the inherited
  // abstract method
  void greetings() {
    System.out.println("Hello World");
  }

}

// main class
public class Example {
  public static void main(String[] args) {

    // instantiate an object
    HelloWorld obj = new HelloWorld();

    // method call
    obj.greetings();

  }
}

Output:

$ javac Example.java 
$ java Example
Hello World
Share

Recently Updated