Java Abstraction

Java Abstraction means to hide some features (implementation details) of the code and display only necessary information to user. Abstraction is a fundamental feature of object oriented programming OOP language. Data abstraction provides the capability to hide implementation details of the data and show only needed information for user interaction to the outside world. 

The real life example of abstraction is a car. Driver has the ability to accelerate, apply breaks and steers. While accelerating, driver just pushes the accelerator. The functionality of how car moves forward is hidden from the driver. Similarly driver just pushes the breaks to stop the car. The mechanics of stopping the car are unknown to the driver. This phenomenon is called abstraction.

Types of Java Abstraction

There are two levels of abstraction in Java:

  • Abstract Classes
  • Abstract Methods

Java Abstract Class

The keyword abstract is used to declare an abstract class in java. An abstract class in java cannot be instantiated which means that programmer cannot create objects of an abstract class. Doing so, will generate an error. An abstract class can have both concrete methods and abstract method. It may or may not contain an abstract method. Abstract class also have default constructors like regular classes.

The syntax of declaring an abstract class is:

Java Abstract Method

The keyword abstract is written before the method name to declare it as Abstract Method. A method that is declared without its implementation is an Abstract Method. Overriding becomes compulsory in an abstract method since it has no body. The subclass should contain the definition of all abstract methods. However, if subclass is not defined then there is no need to override the abstract class.

A class can have one or more abstract methods. The class in which abstract method is declared, is an Abstract class. The syntax for defining abstract method is:

In Java abstraction the superclass is a general form that contains all the information about the subclasses. Here is an example that demonstrates the phenomenon of abstraction.

// abstract class
abstract class Website {
  // abstract method (does not have a body)
  public abstract void websiteName();
  // regular method
  public void greet() {
    System.out.println("Hello! Welcome to, ");
  }
}

// subclass (inherited from Abstract Class)
class WebsiteMessage extends Website {
  public void websiteName() {
    // The body of abstract method is defined here
    System.out.println("Tutorialsart");
  }
}

class Example {
  public static void main(String[] args) {
    // Create an object of subclass
    WebsiteMessage obj = new WebsiteMessage();
    obj.greet();
    obj.websiteName(); // call abstract method
  }
}

In the above example, the code contains an abstract class Website. It is also a superclass. It contains an abstract method websiteName() and a regular method greet() . The method’s definition is present in the subclass. The subclass WebsiteMessage inherits from the superclass. The output of the above code after compilation is:

Java Abstraction can also be achieved through Interfaces.

Advantages of Java Abstraction

There are multiple advantages of using abstraction in Java programming language. Some of them are:

  • Java Abstraction helps user achieve simplicity by concealing the irrelevant and complex details.
  • Programmer can make the internal working of code secure by giving no access of it to the front end user.
  • Abstraction provides the flexibility to change the internal implementation of code without having to change the public data. For example, if a programmer needs to change the private member of a class, he can do it by simply changing it at one place only. Rather than changing it from all the classes those were accessing that public member.
  • By using abstraction, programmer does not have to write low level code.
  • Abstraction provides code reusability and follows the coding principle DRY (don’t repeat yourself).
  • Programmer can avoid code repetition or duplication by using Java abstraction.