0% found this document useful (0 votes)
28 views31 pages

OOP Friday

Uploaded by

Vuong Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views31 pages

OOP Friday

Uploaded by

Vuong Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

AI VIETNAM

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

Mechanism by which one class is allowed to inherit the features


(attributes and methods) of another class.

Super Class: The class whose features are inherited is known as


superclass (a base class or a parent class).

Subclass: The class that inherits the other class is known as


subclass (a derived class, extended class, or child class).

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

- name: string # name: string


- annualSalary: double
- name: string
- yearOfStartingWork: int // …
- insuaranceNumber: string
is
// … // …
Employee

Access modifiers is-a


relationship - annualSalary: double
Employee Person - yearOfStartingWork: int
- private
- insuaranceNumber: string
+ public
An employee is a person.
# protected // …
AI VIETNAM
All-in-One Course
Inheritance
Inherit attributes and methods Animal
from one class to another
Benefit: Code reusability # name: string

+ setName(string): void

Derived class (child) - the class is


that inherits from another class
Cat
Base class (parent) - the class
being inherited from
DerivedClass(BaseClass) // …

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’

‘#’ stands for ‘protected’ is

‘+’ stands for ‘public’ Subclass Manager

- bonus: double

What features does a manager inherit? + computeSalary(): 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”.

Year 2022 https://beginnersbook.com/2013/05/java-abstract-class-method/ 45


AI VIETNAM
All-in-One Course
Inheritance
❖ As a template

Animal

+ sound(): void

is

Dog Horse Lion

+ sound(): void + sound(): void + sound(): void

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

Math1: super class or


parent class
+ __init__()
+ isEven(int): bool
Math2: child class or + factorial(int): int
derived class

Child classes can use


Math2
the public and protected
attributes and methods
of the super classes.
+ __init__()
+ estimateEuler(int): double
Year 2022 52
53
54
AI VIETNAM
All-in-One Course
Another Example
Employee-Manager Example: Simple requirement
Employee

A standard employee of company X includes his/her name and - name: string


base salary. For example, Peter is working for X, and his base - salary: double
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. + computeSalary(): double

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

Shape does not know how to


compute its perimeter and area
+ perimeter(): double
Use @abstractmethod to ask its + area(): double
child to implement them
is
Using pass in the abstract method

Square Circle

- side: int - radius: double

+ __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

Rectangle Square Circle

- height: int
- side: int - radius: double
- width: int

+ __init__(int, int) + __init__(int) + __init__(double)


AI VIETNAM
All-in-One Course
Overriding

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

+ perform(): void + perform(): void


Exercise Vector 𝑥Ԧ =
2
3
𝑢=
4
4
+ 𝑥Ԧ =
6
7
A vector in 2D includes x and y. - x: int 4 3 7
𝑦Ԧ = 𝑣Ԧ = + 𝑦Ԧ =
- y: int 2 3 5
1) Implement a constructor with no
parameters. This constructors set 1
to x and y. + __init__(int, int)
+ add(Vector): void
2) Implement a constructor with two + dotProduct(Vector): int
parameters x and y. + check(Vector): bool 𝑢
3) Implement a destructor

4) Write a method to compute the Dot Product


addition between a vector and
another vector 𝑛 𝑣Ԧ
𝑥Ԧ
5) Write a method to compute the dot x. y = ෍ 𝑥𝑖 𝑦𝑖
product between two vectors. 1 𝑦Ԧ

6) Check if two vectors are the same

You might also like