Chap2 Inheritance
Chap2 Inheritance
CO2039
04 BEST PRACTICES
05 CONCLUSION
06 EXTENSION: COMPOSITION
2
Additional Notes
Default baseclass
Class Object
Overloading method
3
INTRODUCTION TO
01 INHERITANCE
4
What is Inheritance?
A mechanism for creating a new class (called a derived class or child
class) from an existing class (called a base class or parent class) to reuse
and extend its functionality.
5
Why is Inheritance important?
● Code Reusability: Reuse of common logic
from base classes ⇒ codebase more efficient
and maintainable.
● Extensibility: New features can be added by
extending the base class ⇒ easy updates
without affecting existing code.
● Polymorphism: Introduced in Chap 3 later.
● Hierarchical Organization: Classes can be
organized in a logical structure, representing
real-world relationships.
6
KEY CONCEPTS OF
02 INHERITANCE
7
Base class & Derived class
Base class (Parent/Superclass): The class whose properties (attributes)
and methods (functions) are inherited by another class.
Example: Animal is a base class.
Derived class (Child/Subclass): The class
inheriting from the base class. It can use or
override the members of the base class
and add its own ⇒ “is-a” relationship
Example: Dog inherits from Animal ⇒ Dog
“is-a” Animal.
8
Method overriding
A derived class can redefine (override) a method from the base class to
provide specific functionality.
● The method in the base class must have the same name, return
type, and parameters as the overridden method.
Constructors: When an object of a derived class is created, the base
class constructor is called first, followed by the derived class constructor.
10
Types of Inheritance
Diamond
Problem?
11
HOW INHERITANCE
03 WORKS?
12
Subclass inherits all members from Superclass
13
Subclass inherits all members from Superclass
14
Pointers for Polymorphism (1)
class Superclass {
public:
virtual void display() { cout << "Superclass\n"; }
}; Superclass Subclass
int main() {
Subclass sub;
Superclass sup = sub;
sup.display(); // Output: ???
}
15
Pointers for Polymorphism (2)
class Superclass {
public:
virtual void display() { cout << "Superclass\n"; }
}; Superclass Subclass
int main() {
Subclass sub;
Superclass* sup = ⊂
sup–>display(); // Output: ???
}
16
Implicit use of pointers
class Superclass {
public void display() {
System.out.println("Superclass");
}
}
class Subclass extends Superclass { In Java (and many other OOP
@Override
public void display() {
languages): All objects are accessed via
System.out.println("Subclass"); references (similar to pointers in C++).
}
}
public class Main {
public static void main(String[] args) {
Superclass obj = new Subclass();
obj.display(); // Output: ???
}
} 17
04 BEST PRACTICES
18
Exercise 1 - Inheritance in Vehicle classes (1)
19
Exercise 1 - Inheritance in Vehicle classes (2)
Create a derived class Car that inherits from Vehicle:
● Add a private attribute: model (string).
● Provide a default constructor and a parameterized constructor for Car using
constructor chaining.
● Override the displayInfo() method to include model.
Create another derived class Bike that inherits from Vehicle:
● Add a private attribute: engineCapacity (int).
● Provide constructors and override displayInfo() to include engineCapacity.
In the main function:
● Create a Car object using both default and parameterized constructors and display its
details.
● Create a Bike object using both default and parameterized constructors and display its
details.
20
Exercise 2 - Enhanced Library System with Inheritance (1)
Book Class (Base Class):
● Attributes: title, author, ISBN, price, and stock.
● Methods:
○ Constructor to initialize attributes.
○ Getter and setter methods.
○ A virtual displayDetails() method to display book information.
Derived Classes:
● EBook (inherits Book):
○ Additional attribute: fileSize (in MB).
○ Override displayDetails() to include file size.
● PrintedBook (inherits Book):
○ Additional attribute: pageCount.
○ Override displayDetails() to include page count.
21
Exercise 2 - Enhanced Library System with Inheritance (2)
Library Class:
● Attributes:
○ A dynamic array or vector of pointers to Book objects (supporting both EBook
and PrintedBook).
● Methods:
○ addBook(Book*): Add a new book (of any type).
○ searchByTitle(string) and searchByAuthor(string): Search books by title/author.
○ borrowBook(string ISBN): Reduce stock of the specified book by 1.
○ displayAllBooks(): Display all books in the library.
Main Function:
● Add a mix of EBook and PrintedBook objects to the library.
● Simulate user operations: Search for books by title/author, Borrow books, Display all
books.
22
Exercise 3 - Point2D and Point3D
23
05 CONCLUSION
24
Conclusion
1
Introduced in Chap 3 - Polymorphism 25
EXTENSION:
06 COMPOSITION
26
OOP Composition
27
Hands-on exercise
28
Thank you for your
attention!
https://www.cse.hcmut.edu.vn