Inheritance and polymorphism - Computer Science A AP Study Notes
Overview
Imagine you're building a video game with different types of characters: a Warrior, a Mage, and an Archer. They all have some things in common, like a name and health, but they also have unique abilities. Instead of writing the same 'name' and 'health' code for each character, wouldn't it be great if you could just say, 'Hey, all these characters are a type of GameCharacter, so they automatically get the name and health stuff'? That's exactly what **inheritance** lets us do! Then, imagine you have a button that says 'Attack!' When a Warrior clicks it, they swing a sword. When a Mage clicks it, they cast a spell. When an Archer clicks it, they shoot an arrow. The 'Attack!' button doesn't need to know *exactly* what kind of character it is; it just knows it's a character that can 'attack'. This ability to have one action (like 'Attack') behave differently depending on the specific type of object is called **polymorphism**. These two ideas are super powerful because they help programmers write code that's organized, easy to understand, and simple to change or add to. It's like having a well-organized toy box instead of a giant pile of toys!
What Is This? (The Simple Version)
Think of inheritance like a family tree for your code. Just like you inherit traits (like eye color or hair color) from your parents, one class (a blueprint for creating objects) can inherit features (like variables and methods, which are actions objects can perform) from another class.
- The parent class (also called the superclass or base class) is like the parent in the family tree. It has the common features.
- The child class (also called the subclass or derived class) is like the child. It gets all the features from the parent and can also add its own unique features or change how some inherited features work.
So, if you have a Vehicle parent class with a speed and a startEngine() method, a Car child class can inherit speed and startEngine(), and then add its own numberOfDoors variable or honkHorn() method. It's a way to reuse code and organize your programs neatly.
Polymorphism (say: poly-MORF-iz-um) means 'many forms'. It's like having a universal remote control that can turn on different brands of TVs. The 'power' button on the remote always does the same thing (turns on/off), but the way it happens is different for a Samsung TV versus an LG TV. In programming, it means you can treat objects of different child classes as if they were objects of their common parent class, and when you call a method, the correct version for that specific child object will run. It's super cool because it makes your code flexible!
Real-World Example
Let's use our vehicle example. Imagine you're designing a game where you have different types of vehicles:
-
You start with a general blueprint for a
Vehicle. Every vehicle has amake(like 'Ford' or 'Toyota') and amodel(like 'F-150' or 'Camry'). It also has a method calleddrive()that prints 'The vehicle is moving.' -
Now, you want to create a
Car. Instead of starting from scratch, you say, 'ACaris a type ofVehicle.' So,Carinheritsmake,model, anddrive()fromVehicle. But aCaralso has anumberOfSeatsand itsdrive()method might print 'The car is cruising down the highway.' -
Next, you create a
Truck. ATruckis also a type ofVehicle. It inheritsmake,model, anddrive()fromVehicle. But aTruckmight have acargoCapacityand itsdrive()method might print 'The truck is rumbling down the road.'
Now for polymorphism: Imagine you have a list of vehicles in your game, and some are Car objects, and some are Truck objects. You can treat them all as general Vehicle objects. If you loop through your list and tell each Vehicle to drive(), the Car objects will print 'The car is cruising...' and the Truck objects will print 'The truck is rumbling...' even though you just told a generic Vehicle to drive(). The program knows which specific drive() method to use based on the actual type of vehicle. That's polymorphism in action!
How It Works (Step by Step)
Let's break down how inheritance is set up in Java, step by step: 1. **Define the Parent Class:** First, you create the `Vehicle` class with common features. This includes variables (like `String make;`) and methods (like `public void drive() { System.out.println("Vehicle moving."); }`). 2. **Cre...
Unlock 4 More Sections
Sign up free to access the complete notes, key concepts, and exam tips for this topic.
No credit card required ยท Free forever
Key Concepts
- Inheritance: A mechanism where one class (child) acquires the properties and behaviors of another class (parent).
- Parent Class (Superclass/Base Class): The class whose features are inherited by another class.
- Child Class (Subclass/Derived Class): The class that inherits features from a parent class.
- Polymorphism: The ability of an object to take on many forms, allowing a single action to behave differently based on the object's actual type.
- +6 more (sign up to view)
Exam Tips
- โAlways draw a class hierarchy (family tree) when analyzing inheritance problems to visualize relationships.
- โRemember the "is-a" rule: if Class B "is a" Class A, then B can extend A. If B just "has a" A, it's composition, not inheritance.
- +3 more tips (sign up)
More Computer Science A Notes