Abstraction in Java

Thanoshan MV
4 min readMay 1, 2020

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Here’s an example of abstraction: pressing the accelerator will increase the speed of the car. But the driver doesn’t know how pressing the accelerator increases the speed — because they don’t have to know that.

Technically abstract means something incomplete or to be completed later.

In Java, we can achieve abstraction in two ways: abstract class (0 to 100%) and interface (100%).

The keyword abstract can be applied to classes and methods. abstract and final or static can never be together.

I. Abstract class

An abstract class is one that contains the keyword abstract.

Abstract classes can’t be instantiated (can’t create objects of abstract classes). They can have constructors, static methods, and final methods.

II. Abstract methods

An abstract method is one that contains the keyword abstract.

An abstract method doesn’t have implementation (no method body and ends up with a semi colon). It shouldn’t be marked as private.

III. Abstract class and Abstract methods

  • If at least one abstract method exists inside a class then the whole class should be abstract.
  • We can have an abstract class with no abstract methods.
  • We can have any number of abstract as well as non-abstract methods inside an abstract class at the same time.
  • The first concrete sub class of an abstract class must provide implementation to all abstract methods.
  • If this doesn’t happen, then the sub class also should be marked as abstract.

In a real world scenario, the implementation will be provided by someone who is unknown to end users. Users don’t know the implementation class and the actual implementation.

Let’s consider an example of abstract concept usage.

abstract class Shape {
public abstract void draw();
}
class Circle extends Shape{
public void draw() {
System.out.println("Circle!");
}
}
public…
Thanoshan MV

My notes, findings, thoughts and investigations.