Learn how to create specialized classes based on existing ones to reduce redundancy.
If you were building a video game with 100 different monsters, would you want to write the code for 'walking' and 'breathing' 100 separate times? What if you could write it once and let every monster 'inherit' those skills automatically?
In Object-Oriented Programming, Inheritance allows us to create a new class based on an existing one. Think of it like a family tree. The existing class is called the Superclass (or Parent), and the new class is the Subclass (or Child). The subclass automatically receives all the attributes and methods of the superclass. This concept is built on the 'is-a' relationship. For example, a `Dog` is-a `Animal`. Therefore, `Dog` can inherit general traits like `age` or `weight` from the `Animal` class without us having to rewrite that code. This drastically reduces redundancy and makes our software much easier to maintain.
Quick Check
If a 'Smartphone' class inherits from a 'CommunicationDevice' class, which one is the superclass?
Answer
CommunicationDevice is the superclass.
1. Define a Superclass: `Vehicle` with an attribute `speed` and a method `move()`. 2. Define a Subclass: `Bicycle` that inherits from `Vehicle`. 3. Result: The `Bicycle` class now automatically has the `speed` attribute and the `move()` method without you writing a single line of code inside the `Bicycle` class body.
While a subclass inherits everything from its parent, it isn't just a carbon copy. Subclasses can have their own unique features, a process called specialization. Furthermore, a subclass can change how a parent's method works using Method Overriding. Imagine the superclass `Bird` has a method `makeSound()`. A subclass `Duck` would inherit this but 'override' it to specifically produce a 'Quack,' while a `Crow` would override it to produce a 'Caw.' This allows for polymorphism, where different objects respond to the same method call in their own unique ways.
1. Create a superclass `Shape` with a method `calculateArea()`. 2. Create a subclass `Square` that overrides `calculateArea()` using the formula . 3. Create a subclass `Circle` that overrides `calculateArea()` using the formula . 4. Even though both are 'Shapes', they perform the calculation differently based on their specific geometry.
Quick Check
What is the term for a subclass providing a specific implementation for a method already defined in its superclass?
Answer
Method Overriding
A common mistake is using inheritance when you should use composition. To decide, use the 'is-a' test. If you can say 'Class B is a Class A,' use inheritance (e.g., a `Car` is a `Vehicle`). If you should say 'Class B has a Class A,' do NOT use inheritance. For instance, a `Car` has an `Engine`, but a `Car` is not an `Engine`. In that case, you would make `Engine` an attribute inside the `Car` class rather than inheriting from it. Proper hierarchy design ensures that your code structure remains logical and scalable as the project grows.
1. Base Class: `Character` (Attributes: `health`, `name`; Method: `takeDamage()`). 2. Subclass: `Mage` (Inherits from `Character`, adds `mana` attribute, overrides `takeDamage()` to include a magic shield calculation). 3. Subclass: `Warrior` (Inherits from `Character`, adds `stamina` attribute, overrides `takeDamage()` to factor in physical armor). 4. Logic: If a character takes damage and has armor , the Warrior's overridden method might calculate .
Which of the following best describes the relationship between a 'Superclass' and a 'Subclass'?
Using the 'is-a' test, which pair is a valid candidate for inheritance?
A subclass can add new methods and attributes that do not exist in its superclass.
Review Tomorrow
In 24 hours, try to explain the difference between 'is-a' and 'has-a' to a friend using a house and a building as examples.
Practice Activity
Draw a class hierarchy for a 'Bank Account' system. What would the superclass be? What specific types of accounts (subclasses) would inherit from it?