Abstraction is not hiding data/implementation

Abstraction is not hiding data/implementation

WRONG: #Abstraction is hiding background data/implementation.

My Thought: Abstraction in #oops is most misinterpreted topic. I have interviewed more than 150 candidates for Java-Selenium Automation Testing so far. Whenever I ask candidates that, what is Abstraction ? Almost all candidates use to tell me above definition. But I found this definition to be incomplete or wrong at some point.

Abstraction is NOT hiding data:

In java, we use to store data in variables, arrays or collection. Lets say that I am storing data into an variable and making it private. It becomes hidden from other classes in project. But I cannot call it as abstraction.

For example:

public class Test{
  private int i = 10; //Private instance variable  
}

We cannot access 'i' outside the Test class in which it is created.

So surely, abstraction is not hiding data.

Abstraction is NOT hiding background implementation:

Whenever we create an abstract method in a class, it doesn't have implementation. Implementation is provided in Child class who is extending abstract class. So nowhere I see, 'hiding background implementation' (this definition) is justifying the code.

public abstract class Parent{
  public abstract void m1(); //abstract method
}


//It is considered that Child class is written in its own file
public class Child extends Parent{
  
   @Override  
   public void m1(){ //I cannot make this method as private.
       System.out.println("Some implementation");
   }
}

We cannot make overridden method as private to hide it from others because its a rule in Java that we cannot reduce visibility of overridden method in Java.

Then WHAT IS ABSTRACTION:

Abstraction is process of creating a method without implementation and forcing child to use same signature as mentioned in Parent.

It is something like providing Pizza Base instead of fully baked Pizza so that everyone can make Pizza as per their taste keeping Pizza Base intact.

In abstraction, we create a method with signature only and not with implementation. It helps child classes to write implementation without bothering about the signature. Different Child classes will have different implementation. In this way end-user will get multiple choices of same method.

For example, get(int index) method of List interface is implemented differently in #ArrayList and #LinkedList classes in different way. ArrayList's get(int index) method accesses element randomly based index of element. Whereas LinkedList's get(int index) method traverses the list till matching element. This is possible because of Abstraction only.

Some of you might think that, same thing happens with Overriding as well.

The answer is NO. In overriding, parent and child both have different implementations. For example equals(Object o) method of Object class and String class. If you use equals() method of Object class, it will compare hash codes whereas equals() method of String class compares Strings for exact character sequence.

In abstraction, all rules of Overriding shall be applied to Child's implemented method; like:

  1. Method return type cannot be changed
  2. Method name cannot be changed
  3. List of arguments cannot be changed
  4. Access modifier can be changed, but visibility cannot be reduced. For example, if parent class method's access modifier is public then we cannot make it private or default or protected. But if parent class method's access modifier is protected then we can make it public in child class.

That is all ! Let me know what is your thought on this.

I always wondered how abstraction is hiding background data when these methods don't even have a body to begin with. Then what were they hiding! But thanks to this article, now I know why it was bothering me.

Like
Reply
Sahil kumar

Software Engineer | Django | React | Java | Python | SQL | Docker | CSE Grad'23

1y

Very insightful

Hasif Azad

MERN | NodeJS | ReactJS | MongoDB | NextJS | JavaScript | ExpressJS | Java

2y

So the only use of abstraction/abstract class that I can understand from here is to just reserving the method signature. Right?

Liliya Sarokina

PMO | TPM | Autonomy | Robotics | SW | QA | HW | SIM | HITL | ASPICE | SDLC | SAFETY

3y

good article. Write and post here one more quick guide about good old POJO objects and usage of those in complex data sets testing :)

Like
Reply
Aditi Vyas

Aspiring Software Developer| Core Java| SQL

3y

Thank you for clear doubts about abstraction..

To view or add a comment, sign in

More articles by Avinash Pingale

  • What is the problem with FRESHERs

    What is the problem with FRESHERs

    At least in India, freshers can apply for IT jobs if they meet certain academic criteria. Major giant companies in…

  • How to introduce yourself in interviews

    How to introduce yourself in interviews

    To whom you are introducing yourself matters a lot. Your answer to 'Introduce yourself' should vary based on to whom…

Insights from the community

Others also viewed

Explore topics