Abstraction in java

Let’s learn abstraction in java.

Abstraction in java

Abstraction is defined as hiding internal implementation and showing only necessary information.

Also read – inheritance in java

There are two ways to achieve abstraction in java,

  1. through abstract class and
  2. through interfaces

To create an abstract class and abstract method in java “abstract” keyword is used. “abstract” is a non-access modifier.

abstract method in java: An abstract method is a method without body or implementation.

Implementation of abstract method is determined by child class.

A class having an abstract method, that class should be declared as abstract.

Syntax:

// abstract class
public abstract class Calculator
{
   // abstract method
   public abstract int add(int number1, int number2);
}

Here’s abstraction in java with example.

// abstract class
abstract class Addition
{
   // abstract methods
   public abstract int addTwoNumbers(int number1, int number2);
   public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
   // non-abstract method
   public void printValues()
   {
      System.out.println("abstract class printValues() method");
   }
}
class AbstractMethodExample extends Addition
{
   public int addTwoNumbers(int number1, int number2)
   {
      return number1 + number2;
   }
   public int addFourNumbers(int number1, int number2, int number3, int number4)
   {
      return number1 + number2 + number3 + number4;
   }
   public static void main(String[] args)
   {
      Addition add = new AbstractMethodExample();
      System.out.println(add.addTwoNumbers(6, 6));
      System.out.println(add.addFourNumbers(8, 8, 3, 2));
      add.printValues();
   }
}


Output:

12
21
abstract class printValues() method


Let’s see an example on abstract class in java.

import java.util.*;
// abstract class
abstract class Shape
{
   // abstract method
   abstract void sides();
}
class Triangle extends Shape
{
   void sides()
   {
      System.out.println("Triangle shape has three sides.");
   }
}
class Pentagon extends Shape
{
   void sides()
   {
      System.out.println("Pentagon shape has five sides.");
   }
   public static void main(String[] args)
   {
      Triangle obj1 = new Triangle();
      obj1.sides();
      Pentagon obj2 = new Pentagon();
      obj2.sides();
   }
}


Output:

Triangle shape has three sides.
Pentagon shape has five sides.


abstraction in java with realtime example

A realtime example of abstraction is Car. To drive a car steering wheel, gear, accelerator, brake, clutch etc., are shown.

These are necessary to drive a car and are abstract.

But to drive a car internal functionality or working of gear box, disc brake, power steering etc., are not shown.

Also read – polymorphism in java

Because there is no need to know the internal functionality of how gear box or disc brake works for a driver to drive a car.

Thus hiding implementation and showing only relevant data to the user.


Abstract class and abstract methods

  1. We cannot create object of abstract class.
  2. If a class has at least one abstract method then it should be declared as an abstract class.
  3. “abstract” keyword is used to declare an abstract class.
  4. Abstract methods in an abstract class are meant to be overridden in subclass. If not overridden compile time error is thrown.
  5. If a non-abstract class extends an abstract class then non-abstract class must implement all abstract methods of parent class else non-abstract class has to declare as abstract.
  6. An abstract class also consists of default and parameterized constructor.
  7. Abstract class may or may not have abstract methods.
  8. In an abstract class there can be concrete methods.

Advantages of abstraction:

  • increases security of java application since only necessary information is shown to user.
  • code reusability.
  • reduces duplication of code.

Difference between abstraction and encapsulation in java

AbstractionEncapsulation
solves problem at design levelsolves problem at implementation level
shows necessary details and hides implementation to the userhides code and data into a single unit
abstraction is about what object doesencapsulation is about how an object works
for example, abstraction is about car movingfor example, encapsulation is about how car is moving
abstraction is achieved through interfaces and abstract classencapsulation is achieved through access modifiers. protected, private and public