Java Abstraction – Can a Class Achieve Abstraction Without Implementing an Interface?

javaobject-orientedobject-oriented-design

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.

Best Answer

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.