0
0
LLDsystem_design~7 mins

Inheritance and interface notation in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When designing software, unclear relationships between classes cause confusion and errors. Without clear notation, developers struggle to understand which classes share behavior or must implement certain methods, leading to inconsistent code and harder maintenance.
Solution
Using inheritance and interface notation clearly shows which classes inherit behavior from others and which classes promise to implement specific methods. This visual and code-level clarity helps teams understand class roles, reuse code properly, and enforce contracts between components.
Architecture
Interface
(Drawable)
Class A
Class B
Class B

This diagram shows an interface named Drawable implemented by Class A, and Class B inherits from Class A, illustrating interface implementation and inheritance relationships.

Trade-offs
✓ Pros
Clarifies class relationships and responsibilities in code.
Enables code reuse through inheritance, reducing duplication.
Enforces method implementation contracts via interfaces.
Improves maintainability and team communication.
✗ Cons
Overusing inheritance can lead to rigid and tightly coupled code.
Interfaces add extra code and complexity if overused for simple cases.
Misusing inheritance for code reuse can cause fragile designs.
Use when multiple classes share common behavior or must follow a contract, especially in medium to large codebases with multiple developers.
Avoid deep inheritance hierarchies in small projects or when classes have little shared behavior; prefer composition or simpler patterns.
Real World Examples
Google
Uses interfaces extensively in Android development to define UI components contracts, ensuring consistent behavior across different widgets.
Microsoft
In .NET framework, interfaces define service contracts allowing different implementations to be swapped without changing client code.
Amazon
Uses inheritance in backend services to share common logic like logging and error handling across multiple service classes.
Code Example
The before code has classes with draw methods but no enforced contract. The after code defines an abstract interface Drawable requiring draw method, a base class Shape implementing it, and Circle and Square inherit Shape overriding draw. This clarifies relationships and enforces method implementation.
LLD
### Before: No clear interface or inheritance
class Circle:
    def draw(self):
        print("Drawing circle")

class Square:
    def draw(self):
        print("Drawing square")

### After: Using interface and inheritance notation
from abc import ABC, abstractmethod

class Drawable(ABC):
    @abstractmethod
    def draw(self):
        pass

class Shape(Drawable):
    def draw(self):
        print("Drawing shape")

class Circle(Shape):
    def draw(self):
        print("Drawing circle")

class Square(Shape):
    def draw(self):
        print("Drawing square")
OutputSuccess
Alternatives
Composition over Inheritance
Instead of inheriting behavior, classes contain instances of other classes to reuse functionality.
Use when: When you want more flexible and loosely coupled designs avoiding rigid inheritance hierarchies.
Mixin Classes
Small classes provide specific behavior that can be combined with other classes without forming deep inheritance trees.
Use when: When you need to share behavior across unrelated classes without forcing a strict parent-child relationship.
Summary
Inheritance and interface notation clarify class relationships and contracts in software design.
They help enforce method implementation and enable code reuse while improving maintainability.
Use them carefully to avoid rigid designs and prefer alternatives like composition when appropriate.