Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Chapter 3

Object Oriented
Programming
Teacher: Kadache Nabil
Year: 2023/2024

1
Part 1
Classes and
Objects

2
Classes and objects (1)

Definitions.

Object: representation of an entity from the real world, or the virtual world
(in the case of immaterial objects), which are characterized by an identity,
significant states and by behavior.
Identity of an object: descriptor which allows objects to be distinguished from
each other .
State of an object: corresponds to the values of all its attributes at a given
time.
Behavior of an object: is defined by the set of operations that it can execute
in reaction to messages sent by other objects (a message = request to
execute an operation).
Object = <Identity; State ; Behavior>
Class: Abstraction of a set of objects which have an identical

3
Classes and objects (2)

Class = Mold , Model for building objects.


Object = instance of a class.
Creating an object from a class = Instantiation

Example:
Car Class
Attributes:
brand
model
color
year
nserial
Methods :
start()
drive()
stop()
make_Full ()
4
Classes and objects (3)

Attributes/methods of classes and instances


 Instance attributes/methods = ordinary attributes of objects (like
brand, type, start ()… etc ).
 Class attributes/methods (static) have meaning for the class, class
attributes have a single value in their class and for all objects, class
methods can be invoked by their class name.

Example: The number of objects of a class existing at a given time has


no particular meaning for each object, but it has meaning within the
class  a class attribute.
The method that displays this class attribute is also a class method
because it does not deal with objects separately.
5
Classes and objects (4)

Methods and sending messages


 The methods
Methods, defined in a class, represent the dynamic part of the objects
of this class (behavior). A method is defined by a header and a body,
the header is composed of:
• The signature of the method which includes the name of the
method and possibly a list of formal parameters .
• The type of the method's return value.
There are particular methods in a class:
• Constructors : are methods executed (one of them) automatically
during the instantiation of each object.
• The Destroyer: executed automatically when an object is destroyed.
6
Classes and objects (5)

 Sending messages.
In pure OOP the only possible interaction between objects is the
sending of messages.
An object Obj_A sending a message to an object Obj_B means that it
requests the execution of a public method of Obj_B.
Each language uses its own syntax for sending messages, in Java it is
the dot operator (.) which is used:
Obj_B.method (parameter1,parameter2,…);
The object that executes this instruction is the sender object of the
message, Obj_B is its receiver.

7
Classes and objects (6)

Method overloading:
overloading methods in a class allows you to give the same name to
different methods provided they are differentiated by the list of
parameters (in number and/or type).
 advantage: keep the same meaning to the methods which perform
the same actions by acting on different parameters.
 We can overload the constructor (several constructors for the same
class (with different parameters)
 A parameterless constructor is called a default constructor.
 The call form invokes an overloaded method particular (depending
on the actual parameters). Same for constructors (the form of
instantiation decides which constructor to execute).
8
Classes and objects (7)

Abstract methods:
Method which is presented only by its signature (without body).
abstract keyword to declare an abstract method.
Abstract classes and interfaces:
 Abstract class: a class that is not instantiated (you cannot create
objects from an abstract class). Its usefulness is that it allows other
classes to be defined by derivation (inheritance).
 NB: A class which includes at least one abstract method
becomes abstract, in Java, a class preceded by the keyword
abstract in its declaration is an abstract class even if it does not
contain any abstract method.

9
Encapsulation (1)

 Very important principle in OOP

 Any modification of the state of an object (values of its

attributes) must take place exclusively within the methods of


its class (prohibit direct access to attributes from outside a
class).
 Interests: Avoid the side effect (modification of an attribute

at any point in an application, control access to attributes,


security, etc. )

10
Encapsulation (2)
Example:
Let the class model the points of a plane
public class User1 {
audience static void main(String args []) {
Circle c1=new Circle(1.0,2.0,3.0);
c1.x=10.0; // Contrary to the OO spirit
c1.y=10.0; // Contrary to the OO spirit
c1.radius=-1.0; // radius value incorrect
public class Circle { }
f loat radius; }
float _ x;
float y;
public Circle(float r,float a, float b) {
rayon=r; public class User2 {
x=a; public static void main (String args []) {
y=b; Cercle c2=new Circle(2.0,1.0,1.0);
} c2.rayon=-8.0; // valeur de rayon //
} incorrecte
}
}

11
Encapsulation (3)

Problem: the user must control changes to the attributes of the objects he
uses.
Solution: prohibit direct access to the object's state, and provide users with
read and write access methods for each element of the encapsulated state.

In Java: we can define the visibility level of each element of a class.


4 visibility levels can be assigned to each element:
Private (keyword private ): a private element (attribute or method) can only be
accessed in the methods of its class.
Public (public keyword ) : A public element can be accessed anywhere in the
application.
Protected (keyword protected ): can be accessed in its class and in the heir
classes (derived by inheritance from its class).
By default: the attribute can be accessed in its class and in all classes of the
package of its class
NB 1: use the private level for most attributes.
12
Encapsulation (4)

NB 2: most methods are public (available for use) except utility methods and other
methods which can be encapsulated (private).
NB 3: most classes are public, rare classes are non-public and (classes next to a
public class in a .java file which serve as utility classes).
NB 4: class methods ( static ) cannot access instance elements (non-static).

We generally use two methods to access these attributes:


(i) Method for reading values (getters),
(ii) Value modification methods (setters)
ex:
int aaa ; 

private int aaa ;


public void setAaa ( int v) { aaa = v; }
public int getAaa () { return aaa ; }
13
Encapsulation (5)
The code from the previous example can be rewritten using the encapsulation
principle:
public class User1 {
public static void main (String args []) {
public class Circle {
Circle c1=new Circle(1.0,2.0,3.0);
private float rayon;
// c1.x=10.0; to be replaced by:
private float x;
c1. setX (10.0);
private float y;
// c1.y=10.0; to be replaced by:
public Cercle(float r,float a, float b) {rayon=r;
c1. setY (10.0);
x=a;y=b;}
// c1.radius=-1.0; to be replaced by:
public void setRadius (float v) {
c1. setRadius (-1.0)
if (v>0) rayon=v;}
// no modification for radius
public float getRadius() {return rayon;}
}
public void setX(float a) {x=a;}
}
Public float getX() {return x; }
public void setY(float b) {y=b; }
Public float getY () { return y; } public class User2 {
public static void main(String args []) {
} Circle c2=new Circle(2.0,1.0,1.0);
//c2.radius=-8.0; to be replaced by
c2. setRadius (-8.0); // no effect
}
}

14

You might also like