Module 8 Inheritance
2026-04-23 12:02
Tags: #java
Author: Duke Hsu
1 - Key Concepts
1.1 Inheritance definition
Concept:
Inheritance is a mechanism where one class (Subclass) acquires the properties and behaviors (fields and methods) of another class (Superclass). Think of it as a "genetic" transfer of code.
• Superclass (Parent): The class being inherited from.
• Subclass (Child): The class that inherits from the parent.
• Keyword: We use extends to create this link.
• Relationship: It follows the is-arule (e.g., a Dog is-a Animal).
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Class note
1 2 | |
1.2 Using extends Keyword
Concept:
The extends keyword is used to inherit from a parent class.
We use extends to create this link.
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1.3 Method Overriding
Concept:
Method Overriding occurs when a subclass provides a specific implementation for a method that is already defined in its parent class. The method signature (name and parameters) must be exactly the same.
- Why use it? To allow a child class to behave differently while using the same method name.
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Class note:
1 | |
1.4 The super Keyword
Concept:
The super keyword is a reference variable used to refer to immediate parent class objects. It acts as a bridge to reach the "hidden" or "original" parts of the parent class.
Common uses:
super(): Calls the parent's constructorsuper.method(): Calls a method in the parent class that might have been overridden in the child .
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Class note:
1 2 3 4 5 6 | |
1.5 Is-A Relationship
Concept:
- Inheritance follows the
Is-Arelationship
Example:
Dog Is-A Animal
Student Is-A Person
Class diagram symbol for inheritance
- UML Symbol:
Child -----> Parent
Example UML with Is-A Relationship
1.5.1 Has-A Relationship
Concept:
In Java, a Has-A relationship is also known as Association. It occurs when one class uses another class as a member variable . Unlike Is-A (Inheritance) , which focuses on hierarchy, Has-A focuses on Code Reusability and Modularity.
Types of Has-A relationship
There are two main ways to implement a Has-A relationship, based on how strongly the objects are tied together:
A. Aggregation ( Weak Association)
The child object can exist independently of the parent.
- Real-world Example: A school has Students. If the school closes, the students still exist
- UML Symbol: Empty diamond ( \(\diamond \text{---}\) )
B. Composition (Strong Association)
The child object cannot exist without the parent. It represents a "death relationship ".
- Real-world Example: A human has a Heart. If the human dies, the heart (in a functional sense) ceases to function within that context.
- UML Symbol: Filled diamond (\(\blacklozenge \text{---}\))
Why use Has-A
- Flexibility: You can changer the behavior of a class at runtime by swapping the internal object.
- Low Coupling: Classes remain independent. A change in the Engine class doesn't necessarily force a rewrite of the Car class.
- Maintenance: Smaller, focused classes are easier to debug than one giant inherited tree.
UML
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Summary Table
| Feature | Is-A (Inheritance) | Has-A (Association) |
|---|---|---|
| Keyword | extends | new ClassName() |
| Relationship | Tight Coupling | Loose Coupling |
| Meaning | A subclass is a specialized version of a superclass. | A class owns or uses another class. |
| Design Rule | Use for "Type" hierarchy. | Use for "Features/Components." |
Tips
Design Principle: Always "Favor Composition over Inheritance." If you can achieve a feature by including an object rather than inheriting from it, choose composition for better long-term scalability.
1.6 Designing Class Hierarchies
Proper hierarchy improves reusability .
Concept:
Designing a hierarchy means organizing classes from most general to most specific . In UML diagrams, an arrow points from the Child to the Parent to represent inheritance.
Summary Table:
| Term | Simple Definition | Key Requirement |
|---|---|---|
| Inheritance | Reusing code from another class. | Uses extends. |
| Overriding | Redefining a parent's method in the child. | Same name, same parameters. |
| Overloading | Creating multiple methods with the same name. | Same name, different parameters. |
| super | Accessing parent class members. | Used inside the subclass. |
References
Class
