1

I know the architecture, how we can implement abstraction using the interface and an abstract class. But from whom we are hiding the implementation?

As a developer, anyone can click on that method and can see the classes which are giving implementations for those abstract methods.

As a user, he will be only facing UI, so he is not going to see the code anyway.

Then my question from whom we are hiding the implementation?

Abhijeet Gite
  • 79
  • 1
  • 6
  • We aren't hiding it in a security sense, but to stop people writing code which depends on implementation details: https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface?r=SearchResults&s=1|204.3958 – tgdavies Aug 22 '20 at 06:01
  • Sometimes as a developer, you will be working on a big project. There will be other developers involved as well, "hiding implementation" also make it harder for others to change the data by mistake. – Stephan Aug 22 '20 at 06:04
  • @tgdavies Depending on the specifics, it can in fact be a security matter, even in Java. – chrylis -cautiouslyoptimistic- Aug 22 '20 at 06:19
  • Dividing the world into **developers** and **users** is misleading; all developers _are_ users of other developers' work. In a complex application, I'm often the developer of a piece that I then turn around and consume from another layer, several times in sequence. – chrylis -cautiouslyoptimistic- Aug 22 '20 at 06:20

1 Answers1

2

We are hiding it from clients of the interface, so that they don't depend on things which might change. A client of an interface is a piece of code which calls the interface.

Let's say I write a stack interface:

interface Stack {
  push(String s);
  String pop();
}

and an implementation:

class StackImpl {
  public List contents;
  public void push(String s) {
     ...
  }
  ...
}

If clients use StackImpl, they might start accessing the contents directly, which will mean that I can't change the implementation to use an array without breaking all the clients. If they only use Stack, we can improve the implementation without affecting the clients.

tgdavies
  • 10,307
  • 4
  • 35
  • 40