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.

SR Bhaskar
  • 143
  • 5
  • 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?](https://softwareengineering.stackexchange.com/questions/16070/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
  • I would defend my question as a specifically about the mechanisms available in modern programming languages to achieve abstraction. Commonly abstract classes and interfaces are mentioned all over the place (even in programming books), but not public methods. – SR Bhaskar Oct 12 '17 at 15:24
  • 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
  • So in other words there are more than just these three ways (public methods, abstract classes, interfaces) to achieve abstraction in even a simple program. I guess the common answer that one can achieve abstraction in Java thru abstract classes and interfaces is misleading, then. – SR Bhaskar Oct 12 '17 at 15:32
  • No, that's a true statement. You can most certainly achieve abstraction in Java through abstract interfaces and classes. But there are also other ways to achieve abstraction in Java. It would only be a false statement if it said that you can ***only*** achieve abstraction in Java through abstract interfaces and classes. – Robert Harvey Oct 12 '17 at 15:39
  • Well to me that statement seems to miss the bigger picture of what abstraction really is and the myriad of ways that it can be achieved in a program.... but anyway... now I know what to say in my next interview! Thank you. – SR Bhaskar Oct 12 '17 at 15:49
  • Unfortunately, the word "abstraction" has been abused lately by programmers, to the point where people are no longer able to communicate well using that term. Abstraction is used in programming and many other areas of life. It simply means to use a concept via only part of its attributes, while ignoring others: "cat type of animal" is an abstraction, referring to the common parts of "cat" while ignoring specific cat-differences. So as others said, functions are abstractions, and there are myriad abstractions in programming. – Frank Hileman Oct 12 '17 at 17:19
  • 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

1 Answers1

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.

Erik Eidt
  • 33,282
  • 5
  • 57
  • 91