Abstraction in Java – Difference between Abstraction & Encapsulation in Java


1. Objective

In our last Java tutorial, we discussed Method Overloading vs Overriding in Java. Today, we are going to learn about the concept of Abstraction in Java and different methods used in Java Abstraction with its advantages. At last, we will abstraction in Java with example.

So, let’s start Abstraction in Java Programming Language.

Abstraction in Java - Introduction

Introduction to Abstraction in Java

2. What is Data Abstraction in Java

Before we define Abstraction in Java, let’s revise Key features of Java.

Data abstraction is an important concept of OOP, it basically hides the information and provides only that information which the programmer wants the user to know. The trivial information for the user is kept hidden. A very good example would be an automobile in which only those information is visible to us which we need to know.

It basically is the process to identify the important characteristics and ignoring the irrelevant ones. It helps in grouping and classifying the data easily.

Abstraction in Java can be achieved by using abstract classes and interfaces.

3. Some Important Points about Abstraction in Java

a. Abstract Classes and Methods

  • An abstract class is a Java class which is declared using ‘abstract’ keyword and an abstract method is a method which has no implementation.
  • It is not necessary for an abstract class to have all methods as abstract, some of them can be concrete too.
  • A method which is abstract should be redefined in the subclass, this makes overriding compulsory. Another way to resolve this is to define the subclass also as abstract.
  • An abstract class cannot have an object, i.e. it cannot be instantiated with a new operator.
  • An abstract class always has a default constructor, it can also have a parameterized constructor.

b. When to use Abstract Class

Sometimes while implementing a given structure of a superclass we do not want to share the complete implementation of every method, which means the superclass only provides the generalized form to the subclasses leaving it for the subclasses to fill the details.

For example, a given superclass shape, which can be of different type, can have different properties, color, size and so on. There can be many types of shapes like circle, triangle, square and many more, each having its own properties. The hierarchy type can classify both similarity and differences differently.

Abstraction in Java - When to use abstract classes

Abstraction in Java – When to use abstract classes

Example

abstract class Shape
    {
      String color;
      abstract double area();
      public abstract String toString();
      public Shape(String color)
          {
            System.out.println("Shape constructor called");
            this.color = color;
          }
     public String getColor()
         {
            return color;
         }
   }
class Circle extends Shape
     {
       double radius;
       public Circle(String color,double radius)
          {
            super(color);
            System.out.println("Circle constructor called");
            this.radius = radius;
          }
@Override
           double area()
            {
               return Math.PI * Math.pow(radius, 2);
            }
@Override
         public String toString()
            {
               return "Circle color is " + super.color +
               "and area is : " + area();
            }  
     }
class Rectangle extends Shape
  {
     double length;
     double width; 
     public Rectangle(String color,double length,double width)
       {
// calling Shape constructor
          super(color);
          System.out.println("Rectangle constructor called");
          this.length = length;
          this.width = width;
      }  
@Override
         double area()
           {
              return length*width;
           }
@Override
        public String toString()
          {
             return "Rectangle color is " + super.color +
             "and area is : " + area();
          }
   }
public class Test
   {
       public static void main(String[] args)
          {
             Shape s1 = new Circle("Red", 2.2);
             Shape s2 = new Rectangle("Yellow", 2, 4); 
             System.out.println(s1.toString());
             System.out.println(s2.toString());
         }
   }

c. Advantages of Abstraction in Java

  • Complexity reduced – Abstraction reduces the complexity of the view of objects.
  • It reduces the duplicity of codes thus increasing reusability.
  • It helps in increasing security as information is kept hidden.

4. Data Abstraction vs Encapsulation in Java

In abstraction, we hide the details while in encapsulation we hide the data, i.e. former is the implementation hiding process while the latter is Information hiding process.

The role of encapsulation in Java is to group together the data and methods which are acting upon the data while abstraction exposes the interface to a user while hiding the details of implementation.

So, this was all about Abstraction in Java – Difference between Abstraction & Encapsulation in Java. Hope you like our explanation.

5. Conclusion

In this tutorial for Java, we learned about the abstraction in Java, abstract classes and methods used in Java abstraction, advantages, a basic difference between encapsulation and data abstraction, and abstraction in Java with example. Furthermore, if you feel any query, feel free to ask in a comment section.

See Also- Java Syntax & Best Java Books

For reference