Abstraction in Java – Explore Abstraction vs Encapsulation in Java

Abstraction in Java is another important feature of OOP’s. Data Abstraction is the process of hiding unimportant details from the user and provide only the important one. Java Abstraction tutorial is specially designed to know about abstraction and abstract class examples.

So, what are you waiting for?

What is Data Abstraction in 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 the 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.

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

Some Important Points about Abstraction in Java

1. Abstract Classes and Methods

  • An abstract class is a class that is declared using ‘abstract’ keyword and an abstract method is a method that has no implementation.
  • It is not necessary for an abstract class in Java to have all methods as abstract, some of them can be concrete too.
  • A method which is abstract should redefine 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.

Do you know how to declare and call methods in Java?

2. 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 types, 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.

Usage of abstract class in Java

Join DataFlair on Telegram

Example-

package com.dataflair.abstraction;
abstract class Shape
{
  String colorOfShape;
  abstract double areaOfShape();
  public abstract String toString();
  public Shape(String colorOfShape)
  {
    System.out.println("Constructor of Shape class called");
    this.colorOfShape = colorOfShape;
  }
  public String getColorOfShape()
  {
    return colorOfShape;
  }
}
class Circle extends Shape
{
  double radiusOfCircle;
  public Circle(String colorOfShape,double radiusOfCircle)
  {
    super(colorOfShape);
    System.out.println("Constructor of Circle class called");
    this.radiusOfCircle = radiusOfCircle;
  }
  @Override
  double areaOfShape()
  {
    return Math.PI * Math.pow(radiusOfCircle, 2);
  }
  @Override
  public String toString()
  {
    return "Circle color is " + super.colorOfShape +
        " and area is : " + areaOfShape();
  }  
}
class Rectangle extends Shape
{
  double length;
  double width; 
  public Rectangle(String colorOfShape,double length,double width)
  {
    //calling Shape constructor
    super(colorOfShape);
    System.out.println("Constructor of Rectangle class called");
    this.length = length;
    this.width = width;
  }  
  @Override
  double areaOfShape()
  {
    return length*width;
  }
  @Override
  public String toString()
  {
    return "Rectangle color is " + super.colorOfShape +
        " and area is : " + areaOfShape();
  }
}
public class AbstractionDemo
{
  public static void main(String[] args)
  {
    Shape shapeObject1 = new Circle("Blue", 8.2);
    Shape shapeObject2 = new Rectangle("Green", 1, 2); 
    System.out.println(shapeObject1.toString());
    System.out.println(shapeObject2.toString());
  }
}

Output-

Shape-Class-Example-Java

Recommended Reading – Java Abstract Data Type in Data Structure 

3. 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. Rules for Abstract Class

  • It must be declared using the abstract keyword.
  • An abstract class can have both abstract as well as non-abstract methods.
  • We can’t instantiate an abstract class.
  • It can have final and static methods.
  • It can have constructors.

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 the 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.

Java Quiz

Summary

Through Data Abstraction we can filter our information and show only what we want. The concept of data abstraction can only be understood by an abstract class. Now, you know why it is one of the best features of OOPs.

Hope, you liked the article, please add your review and suggestions in the comment box below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.