3

I've read a lot of definitions of abstraction and how it is achieved in programming languages such as Java and C++ using interfaces (Java only) and abstract classes.

I understand that abstract classes and interfaces are required to allow multiple classes to provide their own implementations of the abstract methods and thus achieve abstraction.

Can we also consider a class that hides all its implementation details in private methods and provides a set of public methods (from which it calls the private methods internally) as having achieved abstraction as per object oriented design?

This question is specifically related to the mechanisms available in programming languages to achieve abstraction - I see interfaces and abstract classes mentioned commonly but not public methods.

  • Answering your last paragraph: of course. You don't even need private methods: public methods themselves are abstractions. – Robert Harvey Oct 12 '17 at 15:13
  • 1
    Possible duplicate of What is abstraction? – gnat Oct 12 '17 at 15:14
  • 1
    Note that, if you're looking for a formal definition of abstraction, you might not find one. Abstraction is a mental concept; it is more important to understand what it is and why it is important than it is to achieve a legalistic definition of the word. – Robert Harvey Oct 12 '17 at 15:19
  • 3
    Really, almost everything in computing can be considered an abstraction. A variable is an abstraction. A first-class function is an abstraction. A cache is an abstraction. A data bus is an abstraction. A closure is an abstraction. Programming languages are really sophisticated abstractions over machine code. And so forth. Garbage collection is absolutely an abstraction. – Robert Harvey Oct 12 '17 at 15:29
  • 1
    As a rule of thumb, if you can refactor a class into an interface without changing a lot of code, your class was pretty well abstracted to start with. I use this a guiding principle in class design. I have learned this is better than building an interface for every class and wait until I'm sure I need one. – JimmyJames Oct 12 '17 at 19:47
5

Abstraction, or "an abstraction" can range from a simple constant to a set of interfaces, e.g. an entire API.

This range also includes a single function, and also a group of methods (i.e. a class or interface).

The quality of an abstraction goes to its usability and completeness: an incomplete abstraction often requires clients to know more than they should about its implementation detail to make up the short fall, which creates tighter coupled code than we'd like to see.

So, for example, a single constant, while a form of abstraction, is probably not very complete on its own without other constants and methods.

Layering is the alternation of providing an abstraction, whose implementation is in terms of other abstractions. Ideally, the consumer of a given layer (e.g. the layer above) interacts only with that layer and does not need to go to a lower layer.

Abstractions can be packaged in various ways as well: either informally via documentation (e.g. documentation that some such methods work together), or more formally using language constructs, depending on the programming language, of course.

Classes and interfaces are a formal mechanism that groups methods into an abstraction.

Namespaces are a formal mechanism that groups classes and interfaces (and other namespaces) into an abstraction of a larger API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.