DEV Community

Cover image for Abstraction in Java
Hariharan S J
Hariharan S J

Posted on

Abstraction in Java

1.Introduction

Hey guys today in this Blog you are going to learn about one of the OOPS Concepts called Abstraction

Important Note

Before studying or Practising this topic please do Study the OOPS concepts of class,object,Polymorphism,Inheritance and Endcapsulation because The OOPS concepts are deeply intertwined, and understanding one concept helps in understanding the others without studying those topics this topics would be much much difficult to understand so please do ensure to study those topics before comming into this topic

without any further delay let's get started

2.What is mean by Abstraction

first we will discuss this term non technically

Abstract means a 5 to 10 lines Which will discribe the whole Paragraph

for ex : take your college final year abstract in that how's the project working is only is there in abstract the functionalities or the parts of behaviour is not mention in the abstract or in simple words it can be called as a Synopsis

Like that

Abstraction in Java is the process of hiding internal implementation details and showing only essential functionality to the user. It focuses on what an object does rather than how it does it.

  • It hides the complex details and shows only essential features.

  • Abstract classes may have methods without implementation and must be implemented by subclasses.

  • By abstracting functionality, changes in the implementation do not affect the code that depends on the abstraction.

(https://www.geeksforgeeks.org/java/abstraction-in-java-2/)

Real-Life Example of Abstraction

Imagine you are the guy who is Standing in Front of an ATM Machine Like this in the photo , you enter the card in the machine and you click the withdraw option in Machine , and put your Personal Identification Number Which is Shortly Called as PIN Number

You Wait for few seconds after the machine dispence the cash you take the cash and go for your work

but have you ever wondered like what is the internal process going on inside the machine

yeahh your guessing is right it also follows abstraction because u can only see the screen and buttons of the atm machine through your eyes but you cannot see whats going on inside process

2.Why is Abstraction used in Java?

Abstraction is used to hide implementation details and show only the essential features of an object.

Simple Example

Take a car , in car everything in an object like the wiper , tire, Seat belt like that so for a driver which object should important is

  • Steering wheel

  • Acclerator

  • Brake

  • Clutch

  • Gear

The Driver don't need to know how the engine, gearbox, or fuel injection system works internally.

This is abstraction — exposing what to do while hiding how it is done.

Okay now lets see some example Code after seeing the code you can even more connect with these topic called Abstraction

abstract class Indians {

    abstract void haveBreakfast();

    public void applyRationCard() {
        System.out.println("Ration card");
    }

    public void applyDrivingLisence() {
        System.out.println("Driving lisence");
    }
}
Enter fullscreen mode Exit fullscreen mode
class Tamils extends Indians {

    public static void main(String[] args) {

        Tamils thamizhan = new Tamils();

        thamizhan.applyRationCard();
        thamizhan.applyDrivingLisence();
        thamizhan.haveBreakfast();
    }

    public void haveBreakfast() {
        System.out.println("Idly , Dosa");
    }

}
Enter fullscreen mode Exit fullscreen mode

India is a country where Diverse culture is there that's why India is popularly called as "Unity in Diversity"

this code is related to that because in Indians.java i have a method called have method called

abstract void haveBreakfast();

Enter fullscreen mode Exit fullscreen mode

haveBreakfast Method cannot be defined because in south india which Tami nadu , Kerala , Andhra Pradesh and Karnataka eat Different foods whereas north india which is Maharashtra , Uttar Pradesh , Bihar like that they eat different foods for their Breakfast

so the method breakfast is undefined

Rule 1 in abstraction

even if one method is Abstract in one class then the Entire Class Should be Abstract

Rule 2 Object Should not Be created in the Abstract Class

When you are creating Object in the Abstract Class it will show an Error like

abstract class cannot be instantiated
Enter fullscreen mode Exit fullscreen mode

With the help of Inheritance you can create a child class for your parent class and create object for your child and call the non static methods from your child class of your child object

so when you execute this program the output of the program will be

Java provides two ways to implement abstraction, which are listed below:

1.)Abstract Classes (Partial Abstraction)

2.)Interface (provides abstraction for behavior, may contain default or static methods) To Be Discussed

Okay guys i hope somewhat you understand About this Topic if you having Any Doubts Drop it down in the Comment Section

My Final Thoughts About This Topic

You may Watched this movie or you may Heard about this movie called Inception so Basically the movie is all about dreams vs reality esspecially the climax is scene is an ultimate twist Because the Hero of the movie Cob spins of his totem if the totem automatically stops menas the hero is in reality if it is not stopped or else if it is continuously spins means the hero will be in dream

the mad man director christopher nolan takes the scene and in the climax hero spins his totem and go to his child but the biggest question here is wheather the totem is stopped or not

christopher nolan sends it to the audiance perspective let audiance decide wheather the totem is stopped or not

but accoding to technically it is undefined because the creator of the film itself designed like that only

Like wise the same way The Abstraction Applies to the Undefined Methods

See you in my next blog guys ...

Top comments (1)

Collapse
 
buddingdeveloper profile image
Ebenezer

Well Written Blog Hari , Thanks for this ...