Inheriting Features & Defining Contracts (Inheritance & Interfaces)
Learn how child classes inherit parent traits, override method behaviors, invoke parent constructors with super, and enforce mandatory job contracts using Interfaces.
Why rebuild the wheel when you can inherit existing features? Inheritance lets new classes inherit capabilities from existing classes. Interfaces allow us to define strict job requirements that any class must follow.
4.1. 1. Inheritance (extends) & Calling Parents (super)
Definition: Inheritance allows a child class (subclass) to automatically inherit fields and methods from a parent class (superclass) using the extends keyword.
Core Working Principles:
- Reusing Parent Code: Avoids rewriting duplicate code by inheriting existing features from a parent class.
- The
extendsKeyword: Tells Java that a child class is building upon a parent class (e.g.class Car extends Vehicle). - The
superKeyword: Refers to the parent class, used to call parent constructors or parent methods.
Think of family genetics:
• Parent Class (Vehicle): Has basic features like wheels, engine, and a horn.
• Child Class (ElectricCar): Inherits the wheels, engine, and horn automatically, but adds new special features like a Battery Charger!
graph TD
Parent[Parent Class: Vehicle] -->|extends| Child1[Child Class: Car]
Parent -->|extends| Child2[Child Class: Bicycle]
Child1 -->|adds new feature| Spec1[Feature: Air Conditioning]
Child2 -->|adds new feature| Spec2[Feature: Basket Handlebars]
5-Marks Exam Scoring Points
- Inheritance lets child classes reuse fields and methods from parent classes using 'extends'.
- Java supports single class inheritance (a child can have only one direct parent class).
- The super keyword calls parent constructors and methods.
4.2. 2. Method Overloading vs Method Overriding
Definition: Method Overloading provides multiple versions of a method in the same class; Method Overriding lets a child class replace a parent class method.
Core Working Principles:
- Method Overloading: Same method name, but different parameter inputs (e.g.,
add(int a, int b)vsadd(double a, double b)). - Method Overriding: Child class provides a customized version of a method declared in its parent class (marked with
@Override).
| Feature | Method Overloading | Method Overriding 🏆 |
|---|---|---|
| Where it Happens | Inside the **Same Class** | Between **Parent & Child Classes** |
| Method Name | Must be Identical | Must be Identical |
| Parameters | Must be **Different** | Must be **Exact Same** |
5-Marks Exam Scoring Points
- Overloading happens in the same class with different parameters.
- Overriding replaces a parent method in a child class with identical parameters.
- Use @Override annotation to verify overriding signatures.
4.3. 3. Abstract Classes & Contract Interfaces
Definition: Interfaces and Abstract classes allow software designers to set mandatory contracts that classes must implement.
Core Working Principles:
- Abstract Class: A partially completed blueprint that cannot be instantiated directly with
new. - Interface (
interface): A pure behavioral contract. Any class that implements an interface MUST write code for all its promised methods.
Interface Contract: An interface named DriverContract states: "Any person who signs this contract MUST know how to steer(), brake(), and park()."
Whether you drive a Truck, Bus, or Sedan, you MUST implement those 3 required actions!
5-Marks Exam Scoring Points
- Interfaces enforce behavioral contracts across classes using 'implements'.
- Abstract classes provide partial blueprints with some incomplete methods.
- A class can implement multiple interfaces simultaneously.
✍️ Chapter 4 Reference Exercises & Review Problems
- Overloading (Same Class): Like a chef offering the same recipe with different optional ingredients (e.g. `makePizza(cheese)` vs `makePizza(cheese, pepperoni)`).
- Overriding (Parent to Child): Like a child updating their family's secret cookie recipe with their own modern version, replacing the parent's old recipe!
1. Avoiding Conflict: Extending two parent classes causes the Diamond Problem (if both parents have a `start()` method, the child doesn't know which parent to follow).
2. Multiple Interfaces: Interfaces only state method signatures without conflicting code bodies, so a class can safely agree to fulfill multiple behavioral contracts at once!