- Get link
- X
- Other Apps
INHERITANCE vs POLYMORPHISM
Distinguishing Inheritance and Polymorphism in UML Class Diagrams / OOP
1. CORE DIFFERENCE
2. INHERITANCE
Inheritance is a structural relationship between classes.
A subclass obtains attributes and/or operations from a superclass,
and can add or specialize them.
┌─────────────────┐
│ Vehicle │
├─────────────────┤
│ - brand │
│ - speed │
├─────────────────┤
│ + move() │
└────────┬────────┘
△
│
┌───────────┴───────────┐
│ │
┌─────────────────┐ ┌─────────────────┐
│ Car │ │ Motorcycle │
├─────────────────┤ ├─────────────────┤
│ - numberOfDoors │ │ - engineCC │
└─────────────────┘ └─────────────────┘
UML Generalization:
Car ─────────▷ Vehicle
Motorcycle ───▷ Vehicle
Conceptually:
Car IS-A Vehicle.
Motorcycle IS-A Vehicle.
Therefore, inheritance answers:
"What class is this class derived from?"
"What class is this class derived from?"
3. POLYMORPHISM
Polymorphism means that the same operation/interface can produce
different behavior depending on the actual object receiving the operation.
Vehicle
│
├── Car
│ └── move() → moves on four wheels
│
└── Motorcycle
└── move() → moves on two wheels
The operation name remains the same:
move()
But its behavior changes according to the object's actual type.
Vehicle v
v = Car
v.move()
→ Car's implementation
v = Motorcycle
v.move()
→ Motorcycle's implementation
4. THE RELATIONSHIP BETWEEN THEM
INHERITANCE
↓
STRUCTURE / RELATIONSHIP
↓
"Dog is an Animal"
↓
common interface
↓
POLYMORPHISM
↓
BEHAVIOR / SUBSTITUTION
↓
"Animal reference can represent Dog or Cat"
↓
same operation → different implementation
Key Idea:
Inheritance creates the class relationship.
Polymorphism exploits a common interface so different classes can behave differently.
Inheritance creates the class relationship.
Polymorphism exploits a common interface so different classes can behave differently.
5. SIMPLE REAL-WORLD ANALOGY
ANIMAL
│
┌────────┴────────┐
▼ ▼
DOG CAT
Inheritance
Dog IS-A AnimalCat IS-A Animal
Polymorphism
makeSound()
Dog.makeSound()
→ "Woof!"
Cat.makeSound()
→ "Meow!"
The same message
makeSound() produces
different behavior.
6. UML CLASS-DIAGRAM VIEW
┌─────────────────────┐
│ Animal │
├─────────────────────┤
│ + name │
├─────────────────────┤
│ + makeSound() │
└──────────┬──────────┘
△
┌──────────┴──────────┐
│ │
┌─────────────────┐ ┌─────────────────┐
│ Dog │ │ Cat │
├─────────────────┤ ├─────────────────┤
│ + makeSound() │ │ + makeSound() │
└─────────────────┘ └─────────────────┘
│ │
▼ ▼
"Woof!" "Meow!"
Inheritance:
Polymorphism:
Dog ─────▷ AnimalCat ─────▷ Animal
Polymorphism:
makeSound() → Dog behavior / Cat behavior
7. THE MOST IMPORTANT DISTINCTION
FINAL SUMMARY
INHERITANCE = CLASS RELATIONSHIP
A subclass derives from a superclass and inherits/reuses its characteristics.
POLYMORPHISM = BEHAVIORAL VARIABILITY THROUGH A COMMON INTERFACE
Different objects can respond differently to the same operation/interface.
INHERITANCE → WHAT A CLASS IS / RECEIVES
POLYMORPHISM → HOW AN OBJECT CAN BEHAVE
POLYMORPHISM → HOW AN OBJECT CAN BEHAVE
Comments