OOP Friday
OOP Friday
All-in-One Course
Object-Oriented
Programming
(Draft)
Year 2024
Outline
➢ Introduction
➢ Classes and Objects
➢ Iterator and Class Data Type
➢ Inheritance
➢ Exercises
AI VIETNAM
All-in-One Course
Inheritance
❖ Introduction
The subclass can add its own attributes and methods in addition
to the superclass attributes and methods.
Year 2022 40
AI VIETNAM
All-in-One Course
Inheritance
❖ Introduction
Person Employee
- name: string
- annualSalary: double
- name: string
- yearOfStartingWork: int
- insuaranceNumber: string
// … // …
Year 2022 41
AI VIETNAM
All-in-One Course
Inheritance
Person Employee Person
+ setName(string): void
is-a
relationship
Cat Animal A cat is an animal.
43
AI VIETNAM
All-in-One Course
Inheritance
❖ Introduction
Super Class Employee
To extend an existing class
# name: string
UML Annotation # salary: double
+ computeSalary(): double
‘-’ stands for ‘private’
- bonus: double
Year 2022 44
AI VIETNAM
All-in-One Course
Inheritance
❖ As a template
A class Animal that has a method sound() and the subclasses of it like Dog,
Lion, Horse, Cat, etc.
Since the animal sound differs from one animal to another, there is no point
to implement this method in parent class.
This is because every child class must override this method to give its own
implementation details, like Lion class will say “Roar” in this method and
Dog class will say “Woof”.
Animal
+ sound(): void
is
Year 2022 46
AI VIETNAM
All-in-One Course
Example
❖ Implement the two classes below
Math1 Math2
+ __init__()
+ __init__()
+ isEven(int): bool
+ isEven(int): bool
+ factorial(int): int
+ factorial(int): int
+ estimateE(int): double
Year 2022 47
AI VIETNAM
All-in-One Course
Example
Implement the two
classes below
Math1
+ __init__()
+ isEven(int): bool
+ factorial(int): int
Year 2022 48
AI VIETNAM
All-in-One Course
Example
Implement the two classes below
𝑒 = 2.71828
Math2
+ __init__()
+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
Year 2022
AI VIETNAM
All-in-One Course
Example
Implement the two classes below
𝑒 = 2.71828
Math2
+ __init__()
+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
Year 2022 50
AI VIETNAM
All-in-One Course
Example
How to reuse an existing class?
Math1
+ __init__()
+ isEven(int): bool
+ factorial(int): int
Math2
+ __init__()
+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
AI VIETNAM
All-in-One Course
Example
❖ Inheritance
Math1
A manager includes his/her name, base salary, and bonus. The final
salary for the manager comprises the base salary and a bonus. For Manager
example, Mary is a manager in the company. Her base salary and - name: string
bonus are 60000$ and 20000$ a year, respectively. Yearly, she gets - salary: double
paid 80000$ a year. Implement the Manager class and the - bonus: double
computeSalary() method to compute the final salary.
+ computeSalary(): double
Year 2022
AI VIETNAM
All-in-One Course
Another Example
Employee-Manager Example (Using inheritance)
A standard employee of company X includes his/her name and base salary. For example,
Peter is working for X, and his base salary is 60000$ a year.
Implement the Employee class and the computeSalary() method to compute the final
salary for an employee. The salary for an employee is his/her base salary.
A manager is an employee who has the name and base salary attributes. However, the
final salary for the manager comprises the base salary and a bonus.
For example, Mary is a manager in the company. Her base salary and bonus are 60000$
and 20000$ a year, respectively. Yearly, she gets paid 80000$ a year.
Implement the Manager class and the computeSalary() method to compute the final
salary.
Another Example
Employee-Manager
Employee
# name: string
# salary: double
+ computeSalary(): double
is
Manager
- bonus: double
+ computeSalary(): double
57
AI VIETNAM
All-in-One Course
Example
❖ Inheritance recognition
Square
- side: int
Squares and circles are both examples of
shapes. There are certain questions one can + perimeter(): double
reasonably ask of both a circle and a square + area(): double
(such as, ‘what is the area?’ or ‘what is the
perimeter?’) but some questions can be asked
Circle
only of one or the other but not both (such as,
‘what is the length of a side?’ or ‘what is the
radius?’) - radius: double
+ perimeter(): double
+ area(): double
Year 2022 58
AI VIETNAM
All-in-One Course
Example
❖ Inheritance recognition
Shape
Square Circle
+ __init__(int) + __init__(double)
Year 2022
AI VIETNAM
All-in-One Course
Example
❖ Inheritance recognition
60
AI VIETNAM
All-in-One Course
Example
❖ Inheritance recognition
Year 2022
AI VIETNAM
All-in-One Course
Example
❖ Inheritance recognition
Year 2022
AI VIETNAM
All-in-One Course
Example
❖ Exercise
Shape
+ perimeter(): double
+ area(): double
is
- height: int
- side: int - radius: double
- width: int
Employee
- name: string
Overriding is a feature that - salary: double
allows a child class to provide
+ computeSalary(): double
a specific implementation of a
method that is already
is
provided by its super-class.
Overriding
Manager
- bonus: double
+ computeSalary(): double
Year 2022 66
AI VIETNAM
All-in-One Course
Overriding
Example
Person
+ perform(): void
is
Doctor Student