Beginner Reference Manual • Chapter 4

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:

🧬 Layman Analogy: Family DNA & Inheritance

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]
                    
Architecture Specification 4.1: Inheritance Class Hierarchy Graph
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:

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:

📋 Layman Analogy: Driver's License Job Contract

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

Exercise 4.1: Compare Method Overloading with Method Overriding in plain English.
Step-by-Step Explanation for Beginners:
- 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!
Exercise 4.2: Why does Java allow a class to implement multiple interfaces, but only extend one parent class?
Step-by-Step Explanation for Beginners:
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!