Abstraction in Java with Example

In this chapter, we are going to look at examples and explanation of Abstraction in Java.

How to achieve Abstraction in Java?

In Java, there are two ways to achieve Abstraction in Java.

  • Abstract Class (completely abstraction).
  • Interface (partially abstraction).

1. Abstract Class:

A class which is defined as “abstract” is known as Abstract class. We can use an abstract class to achieve abstraction but there are few rules to be followed:

Abstraction in Java

Points to Remember:

  • It can have both abstract and non-abstract method.
  • We can’t instantiate an abstract class.
  • abstract keyword is used to declare a class or method as abstract.
  • If a class contains one Abstract method then the class must be declared as Abstract class.
  • It doesn’t support multiple inheritance.
  • It can have final even static variables.

Example 1:

abstract class A
{
    // Valid, even without any abstract methods
}

class B // Invalid, class B should be abstract since it has an abstract method.
{
    abstract void method();
}

Example 2:

abstract class A
{
   // Abstract class can have multiple abstract and non-abstract methods.

    abstract void method1(); // Valid

    abstract void method2() {} // Invalid - since it has method body

    void method3()
    {
        System.out.println("I am a non-abstract method");
    }

    abstract boolean method4(char z);
}

2. Interface:

The interface is another approach to achieve abstraction in Java. By defining interface we can have multiple inheritance in Java.

Interface in Java

Points to Remember:

  • It supports multiple inheritance.
  • Interfaces are implicitly abstract. Therefore, they don’t need any abstract keywords.
  • An interface can have public static final variables.
  • The interface body can have abstract, default, and static methods.
  • Also, an interface can contain a constant declaration.

Example 1:

//Declaration:
public interface Drawable {
	void draw();
}
//Implementation: Draw method can be used to draw any type of shape  
class Rectangle implements Drawable {
	@Override
	public void draw() {
		// We can add further implementation in draw method.
	}
}

class Circle implements Drawable {
	@Override
	public void draw() {
		// We can add further implementation in draw method.
	}
}

Difference between Abstract Class and Interface?

Abstract ClassInterface
1. An abstract class cannot be instantiated.1. An interface cannot be instantiated.
2. Abstract class doesn’t support multiple inheritance.2. It supports multiple inheritance.
3. It can have both abstract and non-abstract method.3. Interfaces are implicitly abstract.
4. FINAL is not allowed.4. Can’t declare interface as FINAL
5. An abstract class can have a final, private, or static variable with any access specifier.5. An interface can only have static final variables.

When To Use Abstract Class and Interface?

Use Interface when below condition meets your requirement:

  • When you want to use multiple inheritance.
  • Also, when you want to specify the behaviour of a data type. Because it can only have static final variables.
  • When unrelated classes can also implement your interface.

Use Abstract Class when below condition meets your requirement:

  • When you need access modifier other can public like private or protected.
  • Also, when you want to share your code with a similar batch of classes.

I hope you’ve understood Abstraction in Java.