0% found this document useful (0 votes)
11 views34 pages

4 Object

Uploaded by

edoafarelle
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)
11 views34 pages

4 Object

Uploaded by

edoafarelle
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/ 34

OOP

INF211 (INF2112): Object-Oriented class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Programming (OOP)
Objects

Dr. Azanzi Jiomekong


OOP University of Yaounde I, Department
of Computer Sciences
class Patient extends Personne Implements presentYourself {
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){


return this.code ;
November 9, 2022
}
...
}

1 / 34
OOP
class Patient extends Personne Implements presentYourself {
Private String code ;
public void setCode(String code){
this.code=code ;
}
public String getCode(){
return this.code ;
}
...
}

Copyright (c) 2020 Azanzi Jiomekong.


Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation; with
no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. More information about the license is included in the section
entitled ” The GNU Free Documentation License”.

2 / 34
OOP

Objects class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Class

Object
Abstraction

OOPs
concepts
Encapsulation
Polymorphism

Inheritance

3 / 34
OOP

Class class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

UML representation

Object: class
attribute1=value1 :class
attribute1=value1
attribute1=value1

4 / 34
OOP

Class class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

C++ representation: Syntax

5 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

• Basic unit of OOP

• Instance of a class

• Represents the real-life entities

• Has an identity, state and behavior

• Contains data and code to manipulate the data

• Interact with other’s without details on data or code

6 / 34
OOP

Class class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

UML representation

Object: class
attribute1=value1 :class
attribute1=value1
attribute1=value1

7 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

C++ instanciation

i n t main ( ) {
// C a l l i n g t h e d e f a u l t C o n s t r u c t o r o f t h e c l a s s P a s s e n g e r
Passenger passenger1 ;
c o u t <<”The p a s s e n g e r ID i s : ” <<p a s s e n g e r 1 . i d <<e n d l ;
// C a l l i n g t h e P a r a m e t e r i z e d C o n s t r u c t o r
Passenger passenger2 (32);
c o u t <<”P a s s e n g e r ID i s : ” <<p a s s e n g e r 2 . i d <<e n d l ;
// C a l l i n g t h e c o p y C o n s t r u c t o r
Passenger passenger3 = passenger2 ;
}

8 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

• Objects are everywhere

• Unordered collection of related data, of primitive or reference


types, in the form of ”key: value” pairs
−→ Key = variable/function

• Every element is an object whenever it is a function, array, and


string

• Objects are created in two ways:


◦ Using an object Literal
◦ Using an object constructor

9 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

C++ instanciation

%S i m p l e o b j e c t
% l e t school = {
% name : ” V i v e k a n a n d a S c h o o l ” ,
% location : ” Delhi ” ,
% e s t a b l i s h e d : ”1971”
% }
%O b j e c t w i t h f u n c t i o n members
% l e t school = {
% name : ’ V i v e k a n a n d a S c h o o l ’ ,
% l o c a t i o n : ’ Delhi ’ ,
% e s t a b l i s h e d : ’1971 ’ ,
% d i s p l a y I n f o : f u n c t i o n (){
% c o n s o l e . l o g ( ‘ ${ s c h o o l . name} was e s t a b l i s h e d
% i n ${ s c h o o l . e s t a b l i s h e d } a t ${ s c h o o l . l o c a t i o n } ‘ ) ;
% }
% }
% school . displayInfo ();

10 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

U s i n g an O b j e c t L i t e r a l
// D e f i n i n g o b j e c t
l e t person = {
f i r s t n a m e : ’ Robert ’ ,
l a s t n a m e : ’ Danyan ’ ,

// method
getFunction : f u n c t i o n (){
r e t u r n ( ‘ The name o f t h e p e r s o n i s
${ p e r s o n . f i r s t n a m e } ${ p e r s o n . l a s t n a m e } ‘ )
},
// o b j e c t w i t h i n o b j e c t
p ho ne n umber : {
mobile : ’69485658 ’ ,
l a n d l i n e : ’65845545 ’
}
}
U s i n g an O b j e c t C o n s t r u c t o r :
// u s i n g a c o n s t r u c t o r
f u n c t i o n p e r s o n ( f i r s t n a m e , l a s t n a m e ){
this . first name = first name ;
t h i s . last name = last name ;
}
// c r e a t i n g new i n s t a n c e s o f p e r s o n o b j e c t
l e t p e r s o n 1 = new p e r s o n ( ’ J e a n R o b e r t ’ , ’ Koloko ’ ) ;
l e t p e r s o n 2 = new p e r s o n ( ’ A z a n z i ’ , ’ Jiomekong ’ ) ;

11 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

// U s i n g O b j e c t . c r e a t e ( ) method : u s e an e x i s t i n g o b j e c t a s p r o t o t y p e
new o b j e c t

// S i m p l e o b j e c t w i t h some p r o p e r t i e s
c o n s t c ook = {
isCooking : false ,
// D e f i n i t i o n o f t h e f u n c t i o n member o f t h e o b j e c t Cook
p r i n t I n t r o d u c t i o n : function (){
c o n s o l e . l o g ( ’My name i s $ { t h i s . name } . Am I c o o k i n g ? :
${ t h i s . i s C o o k i n g } . ’ )
}
}
// O b j e c t . c r e a t e ( ) method
c o n s t c o o k 1 = O b j e c t . c r e a t e ( coo k ) ;
c o o k 1 . name = ” C h e f G u s t e a u ”
// o v e r w r i t t i n g e x i s t i n g p r o p e r t i e s
cook1 . i s C o o k i n g = t r u e ;
cook1 . p r i n t I n t r o d u c t i o n ( ) ;

12 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation as defined by es6

// C r e a t i n g o b j e c t s w i t h t h e h e l p o f c l a s s e s
// Use t h e c l a s s c o n s t r u c t o r t o c r e a t e t h e o b j e c t

l e t mouse1 = new Mouse ( ’ MIMI ’ , 1 0 ) ;


l e t c a t 1 = new Cat ( ’ Tom’ , mouse1 ) ;

13 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

Inherited properties:
• Properties which have been inherited from object’s prototype
• hasOwnProperty : used to verify if a property is an object own
property

14 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

Property Attributes:
• value: the property’s value
• writable: when true, the property’s value can be changed
• enumerable: when true, the property can be iterated over ”for-in”
• Configurable: if false, attemps to delete the property, change the
property to be an access-or property, or change its attributes will
fail

15 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

// e x a m p l e o f h a s O w n P r o p e r t y u s a g e
c o n s t o b j e c t 1 = new O b j e c t ( ) ;
object1 . property1 = 42;

c o n s o l e . l o g ( o b j e c t 1 . h a s O w n P r o p e r t y ( ’ p r o p e r t y 1 ’ ) ) ; // t r u e

// U s i n g t h e c o n s t r u c t o r
f u n c t i o n Car ( name , maker ){
t h i s . name = name ;
t h i s . maker = maker ;
}
l e t c a r 1 = new Car ( ’ Toyota C a r i n a ’ , ’ Toyota ’ ) ;

// C l a s s e s w i t h c o n s t r u c t o r s
// Only one c o n s t r u c t o r i s a l l o w e d
c l a s s Person{
c o n s t r u c t o r (){
t h i s . name = ” John ” ;
}
}
l e t p e r s o n 1 = new P e r s o n ( ) ;

16 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

• Every JS objects has a prototype object property by default

• All objects created using prototype inherit all properties and


methods from its prototype objects - we can obtain a prototype
chaining

• Prototype chaining terminates with the Object.prototype which is


the default prototype fallback for all objects

• JS objects, by default, inherit properties and methods from


Object.prototype

17 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Javascript instanciation

// c o o k s i s t h e p r o t o t y p e t o c r e a t e o b j e c t s cook
l e t cooks = {
name : ” G u s t e a u ”
}
l e t cook1 = O b j e c t . c r e a t e ( cook )
//To show a l l t h e p r o p e r t i e s o f an o b j e c t
f o r ( l e t key i n person )
c o n s o l e . l o g ( key ) ;
//To d e l e t e a p r o p e r t y
d e l e t e . cook1 . name ;

18 / 34
OOP

Java Script class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Object instantiation

• Creating objects with constructor


◦ Declare a function similar to a class with three parameters
◦ Create an object ”koki” and initialize and call its method

// S i m p l e f u n c t i o n
f u n c t i o n f o o d ( name , r e g i o n , i n g r e d i e n t ) {
t h i s . name=name ;
t h i s . r e g i o n=r e g i o n ;
t h i s . i n g r e d i e n t=i n g r e d i e n t ;
}
// C r e a t i n g an o b j e c t w i t h a c o n s t r u c t o r a s a
// p r o t o t y p e o f f o o d o b j e c t
l e t k o k i = new f o o d ( ’ k o k i ’ , ’ West ’ , ’ b e a n s , palm o i l ’ ) ;
// P r i n t t h e p r o p e r t i e s
c o n s o l e . l o g ( k o k i . name ) ;
//Do t h e same f o r t h e o t h e r p r o p e r t i e s

19 / 34
OOP

Java Script class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Creating object using object literals

Literals:
• Smaller and simpler ways to define objects
• Simple define the property and values inside
// C r e a t i n g o b j e c t s w i t h t h e h e l p o f o b j e c t s l i t e r a l s
l e t koki{
name : ’ Koki ’ ,
coo k : ’ Mami made ’ ,
r e g i o n : ’ West ’
};
// U s i n g a c c e s s o r s
c o n s o l e . l o g ( k o k i . name ) ;
c o n s o l e . l o g ( k o k i [ ’ name ’ ] ) ;
//Add a new p r o p e r t y t o t h e ” k o k i ” o b j e c t
k o k i . i n g r e d i e n t =’ beans , w a t e r , s a l t ’ ;
koki . log ( koki ) ;
// Adding methods a s p r o p e r t i e s
cooking : f u n c t i o n (){
c o n s o l e . l o g ( ’ G r a s e , mix t h e i n g r e d i e n t and p u t on f i r e f o r 2h ’ )
}
koki . cooking ( ) ;
//Add method s t o p C o o k i n g l a t e r t o t h e o b j e c t
k o k i . stopCooking = f u n c t i o n (){
c o n s o l e . l o g ( ’ I f t h e 2h i s o v e r , s t o p c o o k i n g ’ ) ;
}
//20U /s i34
ng the stopCooking f u n c t i o n : koki . stopCooking ( ) ;
OOP

Java Script class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Creating object using Object.create() method

Object.create() method:
• Creates a new object using an existing one as the prototype
c o n s t mouse = {
isEating : false ,
printMyStatus : function (){
c o n s o l e . l o g ( ‘My name i s $ { t h i s . mouseName } . Am I e a t i n g ?
${ t h i s . i s E a t i n g } ‘ ) ;
}
};
c o n s t mouse1 = O b j e c t . c r e a t e ( mouse ) ;
mouse1 = ” mouse1 ” ;
mouse1 . i s E a t i n g = t r u e ;
mouse1 . p r i n t M y S t a t u s ( ) ;

21 / 34
OOP

Java Script class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Creating object using es6 classes

ES6:
• Support class concepts
• Object can be created out of a class
c l a s s Person {
c o n s t r u c t o r ( name , age , s e x , e a t i n g H a b i t ) {
t h i s . name = name ;
t h i s . ag e = a g e ;
t h i s . sex = sex ;
this . eatingHabit = eatingHabit ;
}
}
l e t p e r s o n 1 = new P e r s o n ( ’ B e l l ’ , 5 3 , ’M’ , ” h i g h s u g g a r ” )
console . log ( person1 . eatingHabit ) ;

22 / 34
OOP

Java Script class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Creating object using es6 classes

• Thanks to Node.js, JavaScript can be used as backend language


• JavaScript Engine:
◦ Each browser has its own one
◦ Convert the JS code to make them interpretable by a browser
• Popular JS engines:
◦ Chrome: V8
◦ Firefox: SpiderMonkey
◦ Safari: JavaScriptCore
◦ Microsoft Edge: Chakra/ChakraCore
• ECMA Script Standard:
◦ Trademark scripting-language specification
◦ Standardized by European Computer Manufactures Association
◦ Provides the rules, details, and guidelines that a scripting language
must observe
23 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

• Everything in JS is an object

• Each variable, string, array

• Dynamic Typing: the interpreter figures out the type of the


variable dynamically based on certain conditions

• Primitive Data Types:


◦ Have no methods attached to them
◦ Used in isolation

24 / 34
OOP

JavaScript class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Types definitions in JS

• Data types under the primitive category:


◦ undefined: the variable exist and is not defined
◦ null: variable exist and is not explicitly set
◦ boolean: logical entity which has only two valued (true and false)
◦ number: data type to define a number which can be integer,
floating-point, double
◦ string: defines string values of a character
◦ symbol: primitive data type that can be used to make object
properties that are anonymous

25 / 34
OOP

JavaScript class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Coercion

• Also called typecasting in C++ and Java

• Process of converting a value from one type to another

• Two types:
◦ Explicit coercion:
• The definition of a variable as a data type is explicit
l e t age = 5 3 ;
l e t e x p l i c i t A g e = S t r i n g ( ag e ) ;
l e t e x p l i c i t A g e 1 = a ge . t o S t r i n g ( ) ;

◦ Implicit coercion:
• The interpreter dynamically casts the variable under certain conditions
l e t age = 5 3 ;
l e t i m p l i c i t A g e = a ge + ” ” ;

26 / 34
OOP

JavaScript class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

• The variable lifetime is from where they are declared until their
function ends

• If no function is declared −→ the scope of the variable is global

• When the function is declared, it is usable from anywhere within


the code

27 / 34
OOP

JavaScript class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

• The JavaScript engine works in two different phases:


◦ Creation phase:
• The engine reads through the entire code
• Throw a syntactic error if one is found
• Functions definition are saved in memory
• Variable names are declared
• Variable initialization will not be run
◦ Execution phase: The code is run

28 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Java instanciation

• All the instances share the attributes and the behavior of the class

• The state (defined by the values of the attributes) are unique for
each object

• The new operator is used to instanciate objects

• Four ways to create objects:


1. Using new: Cook = new Cook();
2. Using Class.forName(String className) method
−→ forName: returns the class object associated with the class with
the given string name
−→ cook = (Cook)Class.forName(cm.tsotsa.Cook).newInstance()

29 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Java instanciation

• Four ways to create objects:


1. Using clone() method: create and return a copy of the object
−→ cook2 = (Cook)cook1.clone();

2. Using deserialization: technique of reading an object from a saved


state in a file
−→ FileInputStream file = new FileInputStream(file name)
−→ ObjectInputStream in = new ObjectInputStream(file)
−→ Object obj = in.readObject();

30 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Java instanciation: Anonymous objects

• Instantiated, but not stored in a reference variable


• Used for immediate method calling
• Destroyed after method calling
//When t h e b u t t o n i s p r e s s e d , anonymous
// o b j e c t E v e n t H a n d l e r i s c r e a t e d f o r
j u s t c a l l i n g h a n d l e method
b t n . s e t O n A c t i o n ( new E v e n t H a n d l e r ( )
{
pu bl ic void handle ( ActionEvent event )
{
System . o u t . p r i n t l n ( ” T e s t i n g anonymous o b j e c t s ” ) ;
}
}
)

31 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Python instanciation

An object consists of:

• State: represented by the attributes of an object


Reflects the properties of an object (age, sex, squeen color)

• Behavior: represented by the method of an object


Also reflects the response of an object to other objects (a person is
eating or drinking)

• Identity: gives a unique name to an object and enables one object


to interact with other objects - name of the doc

32 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Python instanciation

c l a s s Person :
#c l a s s a t t r i b u t e
name = ” J i o m e k o n g ”

def init ( s e l f , name ) :


s e l f . name = name

p e r s o n 1 = P e r s o n ( ” Kenneth ” )
person2 = Person (” Asongtia ”)

#A c c e s s i n g t o o b j e c t members
p r i n t ( ” Kenneth i s a { } ” . f o r m a t ( p e r s o n 1 . class . attr1 ))
p r i n t (” Asongtia i s a {}”. format ( person2 . class . attr1 ))

#A c c e s s i n g i n s t a n c e a t t r i b u t e s
p r i n t ( ”My name i s { } ” . f o r m a t ( p e r s o n 1 . name ) )
p r i n t ( ”My name i s { } ” . f o r m a t ( p e r s o n 2 . name ) )

33 / 34
OOP

Object class Patient extends Personne Implements presentYourself {

}
Private String code ;
public void setCode(String code){

}
this.code=code ;

public String getCode(){

...
return this.code ;

Python instanciation

c l a s s Fufu :
#C l a s s a t t r i b u t e s
a t t r 1 = ” Food ”

#I n s t a n c e a t t r i b u t e
def init ( s e l f , name ) :
s e l f . name = name
def foodPresentation ( s e l f ) :
p r i n t ( ”My name i s { } ” . f o r m a t ( s e l f . name ) )
#O b j e c t i n s t a n c i a t i o n
f u f u C o r n = Fufu (” Fufu corn ”)
w a t e r F u f u = F u f u ( ” Water F u f u ” )
#A c c e s s i n g c l a s s methods
fufuCorn . foodPresentation ()
waterFufu . foodPresentation ()

#C r e a t i n g F u f u o b j e c t
f u f u C o r n = Fufu ( ) ;

34 / 34

You might also like