US Trends

how does inheritance relate to abstraction

Inheritance and abstraction are two object-oriented programming ideas that work together: abstraction defines what an object should do at a high level, and inheritance lets more specific classes reuse and refine that abstract definition.

Core relationship

  • Abstraction : You define a general, often abstract type that captures common behavior while hiding unnecessary detail, e.g., an abstract class Shape with methods like area() and perimeter() but no concrete formula.
  • Inheritance : Concrete subclasses like Circle and Rectangle inherit from Shape and provide specific implementations of area() and perimeter(). They reuse any shared code and add or override behavior where needed.

In other words, abstraction gives you a conceptual contract (“every Shape can compute its area”), and inheritance is one of the mechanisms used to realize that contract in reusable, layered code.

How they work together in practice

  • You start with an abstract, high-level parent class or interface that describes what operations exist, not how they are done.
  • You then create a hierarchy of child classes that:
    • Inherit the common properties and methods.
    • Fill in or override the abstract parts with concrete logic.

This combination lets you:

  • Call code through the abstract type (e.g., a list of Shape), while each concrete subclass does its own specialized work.
  • Change or extend behavior by adding new subclasses without rewriting the original abstract type.

Mini example

Imagine a quick story in a codebase:

A team defines an abstract PaymentMethod that promises authorize() and charge(). No one can create a plain PaymentMethod object, because it’s just an abstraction of “something that can pay.” Then they add CreditCardPayment and PayPalPayment classes that inherit from PaymentMethod, reuse any shared logic (like currency conversion), and implement the details of how each system talks to its provider. New methods like CryptoPayment can be added later without changing the rest of the system.

Here:

  • PaymentMethod = abstraction (high-level contract, no direct instances).
  • CreditCardPayment, PayPalPayment = inheritance (concrete specializations that realize the abstraction).

Why this matters

  • Code reuse : Shared logic lives once in the abstract base or non-abstract parent class; children inherit it instead of duplicating it.
  • Modularity and structure : A clear inheritance tree reflects increasing levels of specialization while keeping the high-level abstraction stable.
  • Flexibility : You can program “to the abstraction” (interfaces, abstract classes) and swap in new subclasses later with minimal changes to existing code.

So, inheritance relates to abstraction by turning an abstract, generalized view of a family of objects into a concrete, reusable hierarchy of classes that share a contract but differ in implementation.