I've been doing some research on OOP concepts and am having a bit of an issue in trying to understand what exactly Abstraction is. I've gone over numerous Stack Overflow posts on the topic but haven't really been able to pinpoint a satisfying answer.
I've seen a lot of discussions on the differences between Abstraction and Encapsulation, and naturally started started thinking about Abstraction in terms of hiding how a particular class works and providing abstraction through the class API. Here are some posts that steered me in this direction:
- Best voted answer refers to functions being Abstract. The very next answer starts talking about abstract classes...
- Best voted answer seem to refer to exposing through the class API while the next two goes off in an Inheritance setting. Third answer even suggests Composition and Aggregation is considered an Abstraction
However, as I read through more posts, I noticed answers portraying Abstraction in an Inheritance context, specifically using interfaces and abstract classes to provide an Abstraction of a certain entity (class). I assumed Abstraction given in this manner would allow developers to extend new objects appropriately according to the "guidelines" outlined by this Abstraction. Here are some posts that lead me in this direction:
- First couple of answers talk about Abstraction in an abstract class/interface setting, while some down the line start talking about exposing classes through APIs
- Top two voted answers refer to abstract classes/interfaces
I'm not sure if I'm just completely missing the point here, but it's getting pretty confusing because every answer seems to add a slight variation to the mix. I definitely see why both contexts are crucial in Object Oriented Programming, but I really wanted a clear-cut definition of what Abstraction is.
Which brings me to my point: does Abstraction work in multiple contexts? Does Abstraction portray both of these concepts?
-
Hiding away "unnecessary details" as done through interfaces and abstract classes
- Providing an abstraction on an class to be created through interfaces and abstract classes. We can provide an interface of
IPet
which would act as an abstraction of aDog
class. Additionally, maybe providing anAnimal
base class as an abstract class to provide an additional abstraction?
- Providing an abstraction on an class to be created through interfaces and abstract classes. We can provide an interface of
-
Abstracting the implementation of a class by exposing it through the class API
- given a
Dog
class, we just need to know that it has afeed()
function as part of its API, and call that function to feed it without knowing how the feeding is actually done. This provides abstraction on theDog
class and lets us easily interact with the class
- given a
One of the links I've included above contains the following quote by Matthew Watson that says:
"The problem is that there are no precise definitions for these concepts, and the words themselves have multiple meanings even within the context of object orientation."
Is it just that Abstraction is so abstract, even the definition is abstract :P? Thanks for any guidance in advance!
Aucun commentaire:
Enregistrer un commentaire