Introduction to Inheritance and Polymorphism

Welcome to one of the most exciting parts of Object Oriented Programming (OOP)! If you have already learned about Classes and Objects, you know how to create blueprints for data. But what happens when you want to create a new class that is almost like one you already have, but with a few extra features? Or what if you want to treat a group of different objects as if they were the same type to simplify your code?

That is where Inheritance and Polymorphism come in. These two concepts allow us to write code that is organized, reusable, and incredibly flexible. Don't worry if these terms sound like "computer-speak" right now—by the end of these notes, you'll see they are actually very logical ways to model the real world.

Note: This chapter is part of Theme B.3 Object oriented programming (OOP). While both SL and HL students study these concepts, HL students will apply them to more complex problems.

1. Inheritance: The "Is-A" Relationship

Inheritance is a mechanism where one class (called the Subclass or Child class) takes on the properties and behaviors of another class (called the Superclass or Parent class).

Think of it like a family tree. You might inherit your eye color from your parents. In programming, a Car might inherit general features from a Vehicle. We call this an "Is-A" relationship because a Car is a Vehicle.

Key Terms to Remember:

Superclass (Parent): The existing class that provides the features. It is more general (e.g., Animal).

Subclass (Child): The new class that inherits features. It is more specific (e.g., Dog).

Why use Inheritance?

1. Code Reusability: You don't have to rewrite the same code for every new class. If all Vehicles have a startEngine() method, you only write it once in the Vehicle class, and Car, Truck, and Motorcycle all get it automatically.

2. Maintenance: If you find a bug in the startEngine() logic, you only have to fix it in one place (the Superclass).

Real-World Example:

Imagine a video game with different types of characters:

Superclass: GameCharacter (Attributes: health, name; Methods: move())

Subclass: Warrior (Inherits everything from GameCharacter, but adds swordAttack())

Subclass: Mage (Inherits everything from GameCharacter, but adds castSpell())

Quick Tip: If you find yourself copying and pasting code between two classes, you probably need a Superclass!

Key Takeaway:

Inheritance allows a Subclass to inherit attributes and methods from a Superclass, promoting code reuse and representing "Is-A" relationships.

2. Method Overriding

Sometimes, a Subclass wants to use a method from the Superclass but needs to change how it works. This is called Method Overriding.

Imagine the Superclass Animal has a method called makeSound(). By default, it might just print "Generic animal sound." However, the Subclass Dog wants to print "Woof!" and the Subclass Cat wants to print "Meow!"

When the Dog class provides its own version of makeSound(), it overrides the parent version. When you call that method on a Dog object, the computer will use the "Woof!" version, not the "Generic" version.

Common Mistake: Don't confuse Overriding with Overloading. Overriding happens when a Subclass changes a method it inherited. Overloading (which you may see in B.2 Programming) is having two methods in the same class with the same name but different parameters.

3. Polymorphism: "Many Forms"

The word Polymorphism comes from Greek, meaning "many forms." In OOP, it refers to the ability of different objects to respond to the same method call in their own specific way.

Polymorphism works hand-in-hand with inheritance. It allows us to treat a group of different Subclasses as if they were their Superclass. This makes our code much more flexible.

The "Remote Control" Analogy

Imagine you have a universal remote control with a "Play" button. You can use it on a DVD player, a Blu-ray player, or a streaming box. Even though the "Play" button is the same on the remote, each device reacts differently: the DVD spins a disc, while the streaming box fetches data from the internet. You don't need a different remote for every device; you just need to know they all have a "Play" function.

How it looks in code:

Suppose you have a list (or array) of Shape objects. Some are Circles, some are Squares, and some are Triangles. Because of polymorphism, you can loop through the list and tell every object to draw(). You don't need to check if it's a circle or a square; the computer automatically knows which draw() method to use for each specific object.

Did you know? This is often called Dynamic Method Selection (or Late Binding) because the decision of which method to run happens while the program is actually running, not when it is being written.

Key Takeaway:

Polymorphism allows us to use a single interface (like a method name) for different underlying types, making the code easier to expand and change later.

4. Summary and Comparison

It is easy to get these two confused, so let's look at them side-by-side:

Inheritance is about Structure. It builds a hierarchy to share code. (Example: A Manager inherits from Employee).

Polymorphism is about Behavior. It allows the program to decide which version of a method to use at runtime. (Example: Both Manager and Clerk have a calculateBonus() method, but they calculate it differently).

Quick Review Box

1. Superclass: The "Parent" class (General).

2. Subclass: The "Child" class (Specific).

3. "Is-A" Relationship: If you can say "A [Subclass] is a [Superclass]", inheritance is appropriate.

4. Overriding: Replacing a parent's method with a specific version in the child class.

5. Polymorphism: Calling the same method name on different objects and getting different results.

5. Moving Forward (HL Depth)

For HL students, you will apply these concepts in Section B.3 "Applying OOP to a problem". You will be expected to read and write code in either Java or Python that uses these features. Remember:

- In Java, we use the extends keyword for inheritance.
- In Python, we put the Parent class in parentheses when defining the Child class, like class Dog(Animal):.

Don't worry if this seems tricky at first! The best way to master inheritance and polymorphism is to try building a small "Family" of classes yourself. Start with something simple, like types of Food or types of MusicalInstruments, and see how much code you can save by using a Superclass!