Abstraction and encapsulation
Encapsulation picks up where abstraction leaves off. Abstraction says, “You’re allowed to look at an object at a high level of detail.” Encapsulation says, “Furthermore, you aren’t allowed to look at an object at any other level of detail.”
— Code Complete
So, what do we see here?
Abstraction is more about clustering similar objects: abstract watches means nothing specific about mechanism of watches, it can be electronic, quartz, or authomatic.
Mechanism of implementation in OOP: abstract class.
Encapsulation is more about providing interface of interaction: you can read time from watches, recharge it, take it with you, but what is most important here — without looking at details of implementation.
Automatic and Quartz watches look the same from outside, but mechanically are different inside.
Internal parts of watches are encapsulated.
Mechanism of implementation in OOP: interface.
What does it mean practically?
Lower level of abstraction provides more detailed interface:
- Abstractly, all the watches are watches, there can’t be detailed interface, all we know at this level — it’s that watches showTime()
- Then we can define lower level of abstraction: chronometer watches. Here we know for sure, interface will provide start()
and stop()
functions
Objects with same interface can be replaceable: depending on situation, you might want to replace your mechanical chronometer with electronic one
Objects from lower level of abstraction can replace objects on higher level: if you need any kind of watches (just to see the time), chronometer will work for you, because it has showTime()
and, as an addition to it, start()
and stop().
Here is the link to LSP