Chapter 03 OOP Class and Object
Chapter 03 OOP Class and Object
Programming (OOP)
Dr, SERHANE Oussama
[email protected]
Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes (ESI-SBA)
Second Year CPI
2023/2024
Abstraction
Object
OOP
Concept
Encapsulation
Inheritance
Polymorphism
• Objects: are essential data fields with distinct structures that the programmer can
determine. Once we call an object, the program creates an instance generated from a class.
• Methods: encapsulate functions that handle the data and behavior of an object instance.
• Attributes: Attributes (or variables in PP) refer to characteristics/proprieties of the
object.
Methods
Get_clients()
Set_Options()
Get_clients()
………….
printStatement()
Class Name
Data Members
(Static Attributes)
Members Functions
(Dynamic Operations)
Person
Name: String
age: int Java code
showName()
showAge()
Student Car
Name: String MatNumber: String
SchoolarYear: int Type: String
grade: char EnginePower: Str
Model: String
getName()
move()
printGrade()
accelerate()
getGrade()
Vehicle
regNo: String
marke: String
Type: String
yearOfManif: int
value: double
Vehicle(String, String, String, int, double)
calculateAge(int):int
getYear():int
setValue(double)
calculateAge(int):int
getYear():int
setValue(double) Object-Oriented Programming Esi-sba 2023/2024 27
Object in Java
Calling Constructor
30
How to use constructor to initialize an object ?
Access to each
object methods
execution
results
36
Object instants Vehicle Challenge
TODO:
1. Complete the
constructor code
(Starting from line
12)
37
Object instants Vehicle Solving
1. Constructor Code
40
Vehicle Class java code
41
Main program
42
Object Association in Java Challenge
Give a class diagram of the following statements:
• an airplane is driven by two pilots, each pilot characterized by its name,
age, Grade, flying hours. An airplane is characterized by it registration
number, altitude and its speed.
• Add the following methods to each of the classes: fly(), land(),
Print_Pilote_Infos(), Update_piloteHours(String).
fly() Print_Pilote_Infos()
Update_piloteHours(String)
land()
1. Give a java code for each class
2. Create the following object:
Pilote 1 : "ahmed“ has 55 year his grad is "g1", he flys "1000“ hours
Pilote 2 : “amine“ has 25 year his grad is “c1", he flys “200“ hours 44
Graphical representation of Class (Association and multiplicity)
Avion
Pilote
regNo: String 1 fly 2 name: String
altitude: String age: int
speed: String Grade: String
piloteHours: String
fly() Print_Pilote_Infos()
land() Update_piloteHours(String)
46
Class & Object: Summary
• A class is a data type. It defines a template for an object
• Class does not occupy memory location.
• Object is an instance of a class.
• Object occupies memory location at run time.
• In java is used to create an instance of the class (Object) we
use the Constructor.
• Constructors are similar to methods except: its name is the
same as the class name and it has no return type.
Object-Oriented Programming Esi-sba 2023/2024 47
Book references