• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is it cool to hide implementation details?  RSS feed

 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got an advice recently to use:

instead of

in order to hide implementation details. I don't understand why is it so cool to hide something, why is it required?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's not required.

However, if you in the future decide you don't want to use an ArrayList, but something else, you don't have to change nearly as much in the former as the latter.

Also, remember that a lot of the time in the 'real world', you may not ever know what your getting back. you buy code from a vendor, or work with another team. They give you a method that effectively says "get me an object to do something". You really don't need to know exactly what it is - you just need to know what methods you can call - which is what an interface is for. Then they can change the details behind the scenes as much as they want, and it will never require you to change your code.
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I understand.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also most of the time it is not necessary to know the internal details. For instance if I have an interface Powerplant, do I really need to know if it's an nuclearpowerplant or a coal powerplant. The only thing that I need to know is how much energy it can supply. The rest isn't really interesting.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it as if..... when we are about to drive the car... system in car asks us "sir, how much rpm you want me to start at?, do you want to know friction in between cylinder and shaft ? at what rate should i burn fuel ? "

As end user we just want to know how to start the car, how to get it going. So to avoid complexities and insignificant data to reach end user, hiding of data is required.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!