Abstraction in Java with Example – Easy Detailed Guide

By | September 27, 2019

Abstraction is an important concept of OOPs. It mainly focuses on the security of a program or application that you are developing.

Abstraction-in-java

What is Abstraction?

We use this term in general day to day life as well. Abstraction means hiding irrelevant things and showing them only the things that matters the most. In programming, we use Abstraction to hide the implementation of the internal code due to security and show only the relevant method to the user.

Example of Abstraction

Ex -1:- Let’s say you are playing the video game. While playing the game, you can operate the game using a few buttons on your console. Each of those buttons performs some action in the game.
While writing code for a video game, we don’t show the internal working or implementation of the game. We provide user highest level of abstraction by which he can only operate the whole game using few keys.

Ex -2:- In Real software projects, we use abstraction heavily in our code. Let’s say we want to make API calls to get some data from our server. For that purpose, we will write create an internal abstraction in our code. So, that every time, when we need to make an API call. We just have to pass fewer details and we will receive output based on that.

Also, Read – Guard Statement in Swift 5

How to achieve Abstraction in Java?

We can achieve Java Abstraction using Interfaces and Abstract Classes. Both of them have slightly different use cases.
Data Abstraction in Java

  • Interfaces – By using Interfaces, we can achieve full abstraction in our project.
  • Abstract Classes and Methods – By using Abstract Classes and methods, we can achieve partial abstraction which is needed in many cases as well.

Java Abstraction using Abstract Methods and Classes

In Java, Abstract modifier is applicable only for classes and Methods. We can’t use this modifier on Variables.

public abstract class StudentDetails{
	private String name;
	private int studentId;

	public void publicDetails(){
		System.out.println("Student Name is " + name)
		System.out.println("Student ID is "+studentId)
	}

	public abstract void personalDetails(Int mobileNo, String bloodGroup);
}

public class AdmissionDep extends StudentDetails{
	private int mobileNo;
	private String bloodGroup;

	@Override
	public void personalDetails(Int mobileNo, String bloodGroup){
		this.mobileNo = mobileNo;
		this.bloodGroup = bloodGroup;
		System.out.println("Mobile no is "+ mobileNo);
		System.out.println("Blood Group is "+ bloodGroup);
	}
}

In the above Java Abstraction Code example, we used an Abstract class StudentDetails. We inherited that StudentDetails class to AdmissionDep class.

When we inherit an Abstract class, it is mandatory to define each of the abstract methods in the parent class. In our code, we defined the personalDetails method in it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.