Abstraction OOP Fundamentals – Interface, Abstract Class, Composition, Open Closed Principle
This article is a definition of terms on abstraction in OOP – including exam questions and tags.
In a Nutshell
Abstraction reduces complex systems to their essential properties and operations, it defines the what of an interface, the how remains hidden and can be developed independently.
Compact Technical Description
Abstraction is the deliberate exclusion of inessential details to obtain a clear, stable model. In OOP it is realized through interfaces, abstract classes, role models and contracts that establish observable behavior. Good abstractions are minimal, complete, consistent, orthogonal, they support interchangeability and test well via black box procedures. Abstraction works closely with encapsulation and polymorphism; abstraction determines what is visible, encapsulation protects implementation details, polymorphism enables interchangeable implementations.
Exam-Relevant Key Points
- Focus on relevant properties, reduction of visible surface
- Means, interface, abstract class, composition, generics
- Separation of contract and implementation, promotes loose coupling and high cohesion
- IHK relevant, precise distinction from encapsulation and inheritance
- Practice, small, role-based interfaces, interface segregation principle
- Security aspect, clear contracts limit attack surface and prevent unvalidated state manipulation
- Efficiency, stable contracts reduce maintenance and change costs
- Documentation obligation, preconditions, postconditions, exceptions, side effects explicitly described
Core Components
- Domain model, ubiquitous language, concepts and relationships
- Contract interface, signatures, semantics, error cases
- Abstract bases, shared core logic without complete implementation
- Roles, multiple small interfaces instead of one god interface
- Composition, has-a relationships for structuring behavior
- Generic types, parametric abstraction over types
- Design patterns, strategy, template method, adapter, ports and adapters
- Architecture boundaries, ports, adapters, bounded contexts
- Quality rules, high cohesion, low coupling, LSP compliant
- Test procedures, contract, substitution, property based tests
Practical Example
// Goal: abstract file export
interface Exporter {
byte[] export(Report r)
}
class PdfExporter implements Exporter {
public byte[] export(Report r) { /* PDF rendering, validation, byte stream */ }
}
class CsvExporter implements Exporter {
public byte[] export(Report r) { /* CSV serialization, separation, encoding */ }
}
class ReportService {
private Exporter exporter
public ReportService(Exporter exporter) { this.exporter = exporter }
public byte[] exportReport(Report r) {
if (r == null) throw new IllegalArgumentException("Report required")
return exporter.export(r)
}
}
Explanation: The Exporter interface defines the abstraction of export, concrete formats are interchangeable, the ReportService remains unchanged.
Advantages and Disadvantages
Advantages
- Lower coupling, higher cohesion
- Better testability, clear responsibilities
- Easy extensibility, more stable APIs
Disadvantages
- Initial modeling effort
- Risk of over-abstracted contracts
- Additional indirection with incorrect design
Typical Exam Questions (with Brief Answer)
-
Abstraction vs. Encapsulation? Abstraction determines which relevant aspects are visible, encapsulation hides the implementation of these aspects behind the surface.
-
Interface vs. Abstract Class? Interface for pure contracts and multiple roles, abstract class when shared core logic or state is needed.
-
Supports Open Closed Principle? Contracts remain stable, new variants emerge as new implementations, existing code does not need to change.
-
Criteria for good abstraction? Minimal but complete, consistently named, orthogonal, stable with internal changes, clearly documented.
-
Check quality of an abstraction? Metrics, number of methods per interface, change frequency, reuse, test coverage, number of implementations.
-
Bad abstraction example? God interface with many unrelated methods, violates ISP, hinders interchangeability and tests.
-
Role of generics in abstraction? Parametric abstraction, one algorithm for many types without casts, type safety through compiler.
-
Polymorphism and abstraction work together? Abstraction defines the contract, polymorphism provides interchangeable implementations, dynamic dispatch binds at runtime.
Most Important Sources
- https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
- https://de.wikipedia.org/wiki/Abstraktion_(Informatik)
- https://martinfowler.com/bliki/RoleInterface.html