Programming Best Practices for Technical Writers – Inheritance

This is the second post in a series dedicated to exploring the fundamentals of programming best practices for technical writers. The first post detailed encapsulation. This one delves deeper into inheritance.

Simply put, inheritance enables a new class (subclass) to inherit the methods and properties of an existing class (superclass). This creates a hierarchical structure of classes, enhancing complexity in the code. Just as a feature in technical writing is introduced through a landing page and followed by supporting articles, a parent class passes down its attributes and methods to child classes. The subclass can also extend or override the parent class’s methods.

Inheritance comes in several forms:

Inheritance TypeDescription
Single InheritanceThe subclass inherits from only one superclass.
Multiple InheritanceThe subclass inherits from more than one superclass (not supported directly in Java, but achievable in Python and C++).
Multilevel InheritanceThe subclass inherits from another subclass, creating a chain of inheritance (e.g., Class A -> Class B -> Class C).
Hierarchical InheritanceMultiple subclasses inherit from the same parent class, sharing common behavior from the parent.

    Given the complexity inheritance can introduce, there are several rules to avoid unnecessary complexity, tight coupling, and the violation of core OOP principles:

    PrincipleDescription
    “Is-A” RelationshipUse inheritance only when a child class is a more specific version of the parent class, representing a true “is-a” relationship.
    Single Responsibility Principle (SRP)Each class should have a single responsibility. Avoid inheritance that forces child classes to acquire unnecessary functionality.
    Liskov Substitution Principle (LSP)Child class objects should be able to replace parent class objects without altering program correctness.
    Open/Close Principle (OCP)Classes should be open for extension (child classes can add behavior) but closed for modification (don’t alter parent class behavior).
    Avoid Deep Inheritance HierarchiesKeep hierarchies shallow to reduce complexity and make maintenance easier.
    Don’t Inherit for Code ReuseUse inheritance to represent logical relationships, not just to share code.
    Respect Parent Class ContractsWhen overriding methods, ensure child classes maintain expected behavior, parameters, and return types of the parent class.
    Avoid Fragile Base Class ProblemBe cautious when modifying a parent class with many subclasses to prevent breaking their behavior.
    Use Abstract Classes for Common BehaviorDefine common behavior in abstract classes that should not be instantiated on their own.
    Favor Composition over InheritancePrefer composition (building classes from components) over inheritance for added flexibility in extending functionality.

    When documenting APIs, classes, or frameworks that use inheritance, it’s essential to explain how subclasses extend or override functionality to help users understand their behavior. Inheritance defines relationships between classes, and understanding these relationships enables the writer to clearly describe how different parts of a system interact or are connected.

    Those are the basics of inheritance. The next post explains a little about abstraction.

    3 thoughts on “Programming Best Practices for Technical Writers – Inheritance

    Leave a comment