Search code examples
abstraction

How to achieve abstraction without using interfaces and abstract classes in java


I was asked this question in one of my interviews and still dont have answer to it. If by abstraction we only mean , you not able to instantiate object(as applicable for interfaces and abstract classes), so having a private constructor is the answer? Abstraction is explained in varied ways all over internet . Even using System.out.println is abstraction as we dont know detail behind it . Using factory classes is also abstraction as we dont know which subclass will be instantiated. Calling any method within an API is also abstraction. I am actually confused now, as to what the interviewer wanted as answer.


Solution

  • Abstraction is all about hiding implementation, like how the gas pedal to a car abstracts you from the various complexities in making a car go.

    A simple way of doing it is to just use private members. GetActiveServers() could call no private methods, or 5 private methods. That doesn't really matter when we're using it, so long as it works efficiently as needed.

    I think he was trying to trick you because of the abstract keyword sort've means something different from abstraction, but it's not entirely unrelated.