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 class Test {
public static void main(String[] args) {
Shape circle = new Circle();
circle.draw();
}
}
Figure 1: Class diagram that shows the relationship between an abstract class and a concrete class

When we want to mark a class as abstract?

  1. Stop having actual objects of that class.
  2. Force sub classes to implement abstract methods.
  3. Keep having class reference.
  4. Retain common class code.

Interface

An interface is a blueprint of a class.

An interface is 100% abstract. No constructors are allowed here. It represents an IS-A relationship.

NOTE: Interfaces only define required methods. We can not retain common code.

An interface can have only abstract methods, not concrete methods. By default, interface methods are public and abstract. So inside the interface, we don’t need to specify public and abstract.

So when a class implements an interface’s method without specifying the access level of that method, the compiler will throw an error stating “Cannot reduce the visibility of the inherited method from interface”. So that implemented method’s access level must be set to public.

By default, interface variables are public, static and final.

For instance:

interface Runnable {
int a = 10; //similar to: public static final int a = 10;
void run(); //similar to: public abstract void run();
}
public class InterfaceChecker implements Runnable{
public static void main(String[] args) {
Runnable.a = 5;//The final field Runnable.a cannot be assigned.
}
}

Let’s see an example that explains interface concept:

interface Drawable {
void draw();
}
class Circle implements Drawable{
public void draw() {
System.out.println("Circle!");
}
}
public class InterfaceChecker {
public static void main(String[] args) {
Drawable circle = new Circle();
circle.draw();
}
}
Figure 2: Class diagram that shows the relationship between interface and a concrete class

Default and Static methods in Interface

Usually we implement interface methods in a separate class. Let’s say we are required to add a new method in an interface. Then we must implement that method in that separate class, too.

To overcome this issue Java 8 introduced default and static methods that implement methods inside an interface, unlike abstract methods.

  • Default method
public interface DefaultInterface {
void sleep();
default void run() {
System.out.println("I'm running!");
}

}
public class InterfaceCheckers implements DefaultInterface{
public void sleep() {
System.out.println("Sleeping...");
}
public static void main(String[] args) {
InterfaceCheckers checker = new InterfaceCheckers();
checker.run();
checker.sleep();
}
}
/*
Output:
I'm running!
Sleeping...
*/
  • Static method

Similar to static methods of classes. We can call them by their interface’s name.

public interface DefaultInterface {
void sleep();
static void run() {
System.out.println("I'm running!");
}

}
public class InterfaceCheckers implements DefaultInterface{
public void sleep() {
System.out.println("Sleeping...");
}
public static void main(String[] args) {
InterfaceCheckers checker = new InterfaceCheckers();
DefaultInterface.run();
checker.sleep();
}
}
/*
Output:
I'm running!
Sleeping...
*/

Marker interface

It’s an empty interface. For instance, Serializable, Clonnable and Remote interfaces.

public interface Serializable 
{
//No fields or methods
}

When do we want to change a class to an interface?

  1. To stop having actual objects of that class.
  2. To force sub classes to implement abstract methods.
  3. To keep having an interface reference.
  4. To achieve multiple and hybrid inheritance.

NOTE: Remember, we can’t retain common code inside the interface.

If you want to define potentially required methods and common code, use an abstract class.

If you just want to define a required method, use an interface.

Now we know about abstraction. If you have any doubts or if you think any other important concepts inside the abstraction is missed here please feel free to let me know. So that we can improve our article and help others to learn easily.

Thank you for reading.

I’ll meet you on the Polymorphism in Java article.

Happy coding!

--

--

Thanoshan MV
Thanoshan MV

Written by Thanoshan MV

Hi! I'm a Software Engineer with a passion for research in AI, software engineering, and open-source development. Web - https://thanoshanmv.github.io/

No responses yet