0% found this document useful (0 votes)
1 views49 pages

Chapter 03 OOP Class and Object

The document outlines a course on Object-Oriented Programming (OOP) taught by Dr. Serhane Oussama at Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes. It covers fundamental OOP concepts such as classes, objects, inheritance, polymorphism, and exception handling, with a focus on Java programming language. The document includes detailed explanations, examples, and exercises related to the creation and management of classes and objects in Java.

Uploaded by

hsmzem123
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)
1 views49 pages

Chapter 03 OOP Class and Object

The document outlines a course on Object-Oriented Programming (OOP) taught by Dr. Serhane Oussama at Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes. It covers fundamental OOP concepts such as classes, objects, inheritance, polymorphism, and exception handling, with a focus on Java programming language. The document includes detailed explanations, examples, and exercises related to the creation and management of classes and objects in Java.

Uploaded by

hsmzem123
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/ 49

Object-Oriented

Programming (OOP)
Dr, SERHANE Oussama
[email protected]
Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes (ESI-SBA)
Second Year CPI
2023/2024

© all right reserved Esi-sba


What is this course about?
Chapter 01 : OOP General Introduction and Motivation
Chapter 02 : Introduction to Java Programming Language
Chapter 03 : Oriented Object Approach: Class and Object
Chapter 04 : Oriented Object Approach: heritage, Polymorphism and Interface.
Chapter 05 : Collections
Chapter 06 : Exception handling and Management
Chapter 07 : Graphical interface
Chapter 08 : Streams and Files
Object-Oriented Programming Esi-sba 2023/2024 2
3
Object-Oriented Programming Esi-sba 2023/2024
Chapter 03: Oriented Object Approach-Class & Object

Object-Oriented Programming Esi-sba 2023/2024 4


Class

Abstraction

Object

OOP
Concept

Encapsulation

Inheritance

Polymorphism

Object-Oriented Programming Esi-sba 2023/2024 5


Chapter 03 Outlines:
 Introduction to OOP
 What is a Class

 Class diagram in UML


 Class in java
 What is an Object
 Object diagram in UML
 Object in java
 Object Association in Java
 Summary
Object-Oriented Programming Esi-sba 2023/2024 6
Introduction

• Procedural programming is about writing functions that


perform operations on the data, while OOP is about
creating objects that contain both data and methods.
• OOP is a way of modeling software where everything is represented as an
Object.
• Object/Objects could be any real-world or even a logical entity like a printing
machine, a car, or a person.
• Each object should have its own data and operations (methods) to perform
on data.
• Each object can communicate with other objects as and when required.
It is similar to the real world
Object-Oriented Programming Esi-sba 2023/2024 7
OOP Basic Concepts

• Solving a problem in an object-oriented language, there is no need to


ask how the problem will be divided into functions, but how it will be
divided into objects.
• Thinking in terms of objects (instead of functions) has
a helpful effect on how easily you can design programs.
• OOP is a paradigm in computer science that strongly
relies on the concept of classes and objects.
• OOP is used to structure a software program into
simple, reusable pieces of code (called classes), which are
used to create individual instances of objects.
Object-Oriented Programming Esi-sba 2023/2024 8
OOP Building blocks
The fundamental building blocks of an OOP program are:
• Classes: offer templates to better characterize objects. classes serve as blueprints for
generating objects.

• 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.

Object-Oriented Programming Esi-sba 2023/2024 9


What is a Class in OOP

Object-Oriented Programming Esi-sba 2023/2024 10


Example Cars using procedural programming: (chapter 01)

New Car Type new


Program Cars1 Program Cars1_2 Program Cars12.. Shape other
Var Var Features ...
Var carName:Str; carName:Str;
carName:Str; carType:Str; carType:Str;
carType:Str; ……… ………
……… carName2:Str; carList:[];
function AddCar(carName, carType …) carType2:Str;
function DeleteCar ():... ………
function SearchCar():... function AddCar2(carName, carType …) function AddCar2(carName, carType …)
………. function DeleteCar2 ():... function DeleteCar2 ():... Edit code
again and
BEGIN function SearchCar2():.... function SearchCar2():.... again !
... function AddCar (carName, carType …) function AddToList()……..
END. function DeleteCar ():... ……….
function SearchCar ():...
………. Object-Oriented Programming Esi-sba 2023/2024 11
Oriented-Object Programming (Thinking):
Class name
Attributes
Single Car Class
Car_name
Car_color
Car_class
Car_Options

Methods
Get_clients()
Set_Options()
Get_clients()
………….

Car Object 01 Car Object 02 Car Object 03 Car Object 04 …. etc

Object-Oriented Programming Esi-sba 2023/2024 12


What is a Class in OOP ?

 The core element of OOP is the class


 Classes are essentially user-defined data types.
 Class is a template definition of the methods and attributes.
 a Class is a group of objects that share common properties and
behavior.
 We can create multiple objects from a class.

Object-Oriented Programming Esi-sba 2023/2024 13


Graphical representation of Class (UML)
A class can be visualized as a three-compartment box, as
illustrated:
Class Name
1.Classname (or identifier): identifies the class.
Data Members
(Static Attributes)
2.Data Members or Variables (or attributes, states,

fields): contains the static attributes of the class. Members Functions


(Dynamic Operations)

3. Member Functions (or methods, behaviors, operations):


contains the dynamic operations of the class.

Object-Oriented Programming Esi-sba 2023/2024 14


Graphical representation of Class (Exercises)
The class is called “Customer”. It has four attributes: name
(which is text), address (also text), balance (a number),
and credit rating (type double). It has one operation:
“printStatement()”, which has no parameters or return
value.
Classname Customer
attributes Name: String
Adress: String
Methods Balance: int
creditRating

printStatement()

Object-Oriented Programming Esi-sba 2023/2024 15


Graphical representation of Class (Examples)
Classname

Student Circle FootballPlayer Car


attributes
Name: String MatNumber: String
Name: String
Radious: double Number: int Type: String
SchoolarYear: int
Color: String xLocation: float EnginePower: Str
grade: char
yLocation: float Model: String
run() move()
getName() getRadious()
Methods jump() park()
printGrade() getArea()
kickball() accelerate()
getGrade()

Object-Oriented Programming Esi-sba 2023/2024 16


What is class in OOP: Association between classes

Object-Oriented Programming Esi-sba 2023/2024 17


Graphical representation of Class (Association and multiplicity)
Student Course
Name: String amount: String
Study
SchoolarYear: int Date: String
* *
grade: char hours: String
getName() getTeacher() Car
update() Person
printGrade()
MatNumber: String
getGrade() Name: String Type: String
Adress: Strin
1 1 EnginePower: Str
…… Model: String
FootballPlayer
Team move()
getName()
Name: String name: String getAdress() accelerate()
Number: int Play
Titles: String ……
xLocation: float * 1 Stadium: String
yLocation: float
Tournament()
run() ………
jump()
kickball()
Object-Oriented Programming Esi-sba 2023/2024 18
Class in Java

Object-Oriented Programming Esi-sba 2023/2024 19


Class in java Syntax example

Class Name

Data Members
(Static Attributes)

Members Functions
(Dynamic Operations)

Object-Oriented Programming Esi-sba 2023/2024 20


Class in java Code example

Person

Name: String
age: int Java code

showName()
showAge()

Object-Oriented Programming Esi-sba 2023/2024 21


Code a class in java challenge
Give the code of the following class in java

Student Car
Name: String MatNumber: String
SchoolarYear: int Type: String
grade: char EnginePower: Str
Model: String
getName()
move()
printGrade()
accelerate()
getGrade()

How to create an object from a class in Java, What is an OOP Object?


Object-Oriented Programming Esi-sba 2023/2024 22
What is a Object in OOP ?

Object-Oriented Programming Esi-sba 2023/2024 23


What is a Object in OOP ?
• An object in OOP represents an entity in the real world, for instance, a
person, a car … etc.
• Many real-world objects have:
 a state (characteristics, or properties that can change).
 behaviors(they can do).
• In Real-world object=State (Properties)+ Behaviors(Abilities)
• in OOP objects = Attributes (Data) + Methods (Functions)
• Examples of states and behaviors of an object:
• Object: Student
State: name, age, gender
Behavior: study, play, run
Object-Oriented Programming Esi-sba 2023/2024 24
Car
Object: Class instance MatNumber: String
Object
Type: String
Student EnginePower: Str
Name: String Model: String
move()
SchoolarYear: int park()
Instance of
grade: char accelerate()
getName()
printGrade() New Object New Object Class
getGrade()
307:Car Higer:Car Hyundai :Car
New Object New Object
Ahmed Amine MatNumber = ”22” MatNumber = ”31” MatNumber = “16”
Type = “Simple” Type = “Bus” Type = “truck”
Ahmed:Student Amine:Student
EnginePower=”1.2” EnginePower=“2.8” EnginePower=“2.8”
Name = “Ahmed” Name = “Amine” Model = “2022” Mode=“2013” Mode=“2010”
SchoolarYear: 2 SchoolarYear: 3 move() move() move()
grade = ’N’ grade = ’P’ park() park() park()
getName() getName() accelerate() accelerate() accelerate()
printGrade() printGrade()
getGrade() getGrade()
The above figure shows three instances
The above figure shows two instances of of the class Car, identified as “307“,
the class Student, identified as “Ahmed" “Higer bus” and “Hyundai truck"
and “Amine"

Object-Oriented Programming Esi-sba 2023/2024 25


Class to java code (Example)
• Transform the following diagram to java class code

Vehicle
regNo: String
marke: String
Type: String
yearOfManif: int
value: double
Vehicle(String, String, String, int, double)
calculateAge(int):int
getYear():int
setValue(double)

Object-Oriented Programming Esi-sba 2023/2024 26


Class to java code (solving)
Vehicle
regNo: String
marke: String
Type: String
yearOfManif: int
value: double
Vehicle(String, String, String, int,
double)

calculateAge(int):int
getYear():int
setValue(double) Object-Oriented Programming Esi-sba 2023/2024 27
Object in Java

Object-Oriented Programming Esi-sba 2023/2024 28


Object in java (Constructor)
Declaring Object Syntax in Java

Ecample declaring Object in Java

Class Name Variable Name

Object instance Syntax in Java

Calling Constructor

What is Constructor in Java class ?


Object-Oriented Programming Esi-sba 2023/2024 29
Declare and initialization Java Object (Constructor)
• Constructor in java is used to create the instance of the class.
• Constructors are almost similar to methods except for two things
 its name is the same as the class name
 it has no return type.

• Constructors are also referred to as special methods to initialize an object.

30
How to use constructor to initialize an object ?

Object-Oriented Programming Esi-sba 2023/2024 31


Constructor Types in java (Example)
• In java there are 3 Constructor Types :
1. Default 2. No-Args Constructor (line 3) 3. Parameterized Constructor (Line 6)

Object-Oriented Programming Esi-sba 2023/2024 32


Constructor Types Example (Default Constructor in Java)

Object-Oriented Programming Esi-sba 2023/2024 33


Initialize an object using Parameterized Constructor

Object-Oriented Programming Esi-sba 2023/2024 34


Output

this keyword refer to the class 35


Creation of multiple Objects instances
Creation of
3 students
Object

Access to each
object methods

execution
results

36
Object instants Vehicle Challenge

TODO:
1. Complete the
constructor code
(Starting from line
12)

2. Add main function


then create 3
Vehicle objects

37
Object instants Vehicle Solving
1. Constructor Code

2. main code and Object creation

Object-Oriented Programming Esi-sba 2023/2024 38


Object Association in Java
Conseduring the folloing Classe diagram where each Vehicle has at least
one Driver which is a Person.
Vehicle Person
regNo: String name: String
marke: String age: int
Type: String 1
1
adress: String
yearOfManif: int PhoneNumber: String
value: double

Vehicle(String, String, String, int, double) Person(String, int, String, String)


calculateAge(int):int Print_Infos()
getYear():int
Cardinality
setValue(double)
39
Todo: Give the java code for each class
Person Class java code

40
Vehicle Class java code

41
Main program

Access to driver info from vehicle Object

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).

Object-Oriented Programming Esi-sba 2023/2024 43


Object Association in Java
Avion Pilote
regNo: String name: String
altitude: String fly
age: int
1 2 Grade: String
speed: String 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)

Object-Oriented Programming Esi-sba 2023/2024 45


Main code

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

Object-Oriented Programming Esi-sba 2023/2024 48


• https://softwaregeekbytes.com/object-oriented-programming-simple-words/


References
https://medium.com/analytics-vidhya/java-object-oriented-programming-55ecbfb1559e
https://techvidvan.com/tutorials/java-class
• https://www.techtarget.com/searchapparchitecture/definition/object-oriented-programming-OOP
• https://www.cl.cam.ac.uk/teaching/0910/OOProg/OOP.pdf
• https://www.tutorialspoint.com/java/java_object_classes.htm
• https://www.digitalocean.com/community/tutorials/constructor-in-java
• https://www.mygreatlearning.com/blog/oops-concepts-in-java
• https://www.fita.in/oops-in-java
• https://www.analyticsvidhya.com/blog/2020/10/inheritance-object-oriented-programming/
• https://docs.oracle.com/javase/tutorial/java/IandI/super.html
• https://www.geeksforgeeks.org/perl-polymorphism-in-oops/
• https://www.mygreatlearning.com/blog/polymorphism-in-java/
• https://www.upgrad.com/blog/polymorphism-in-oops/
• https://www.edureka.co/blog/object-oriented-programming/
• https://www.fita.in/oops-in-java/
• https://www.softwaretestinghelp.com/what-is-abstraction-in-java/
• https://www.digitalocean.com/community/tutorials/what-is-abstraction-in-oops
• https://beginnersbook.com/2013/10/hybrid-inheritance-java-program/
• https://www.techtarget.com/searchapparchitecture/definition/object-oriented-programming-OOP

Object-Oriented Programming Esi-sba 2023/2024 49

You might also like