0% found this document useful (0 votes)
25 views

Poo 2

Uploaded by

Dja Mel
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)
25 views

Poo 2

Uploaded by

Dja Mel
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/ 54

Classes and Objects :

Abstraction /Encapsulation

1
Object Oriented Thinking

• The focus of this course is on class design and


explores the differences between procedural
programming and object-oriented
programming.
• Four main concepts in OOPs are Abstraction,
Encapsulation, Inheritance and
Polymorphism (AEIP)

Object Oriented Programming 2


Class abstraction
• Class abstraction is the separation of class
implementation from the use of a class.
• The creator of a class describes the functions of the
class and lets the user know how the class can be
used.
• The collection of methods and fields that are
accessible from outside the class, together with the
description of how these members are expected to
behave, serves as the class’s contract.

Object Oriented Programming 3


Class abstraction

• What is the need for Data Abstraction? : If you want to hide


your business logic from the outside world. To achieve this
implementation we can use Data Abstraction.
• TV or Car remote is assembled from the collection of circuits
but they don't show to the user all circuits behind the remote,
They only provide remote to the user to use it. When the
user presses the key on remote the channel gets changed.
They provide only necessary information to the user.

Object Oriented Programming 4


Class encapsulation

• The details of implementation are


encapsulated and hidden from the user. This is
known as class encapsulation.
• Class abstraction and encapsulation are two
sides of the same coin.
• Many examples in real life shows the concept
of abstraction/encapsulation – building
computer system, mobile, car.

Object Oriented Programming 5


Class encapsulation

• What is the need for Data Encapsulation? : If


you want to hide the complexity and provide the
security to data. To achieve this implementation
the OOP concept came i.e. Data Encapsulation.
• Remote is assembled from the collection of
circuits but they hide all circuits from the user.
They encapsulate all circuits in one thing called
remote and provide to the user to use it. And also
remote can provide security to all circuits used
inside the remote.

Object Oriented Programming 6


Class encapsulation/Abstraction

• Abstraction hides details at the design level,


while Encapsulation hides details at the
implementation level.

Object Oriented Programming 7


Classes
Classes are constructs that define objects of the
same type. A Java class uses variables to define
data fields and methods to define behaviors.

Additionally, a class provides a special type of


methods, known as constructors, which are invoked
to construct objects from the class.

8
Classes

9
UML Class Diagram

10
Objects
An object has a unique identity, state, and
behavior.

The state of an object consists of a set of data


fields (also known as properties) with their
current values.

The behavior of an object is defined by a set


of methods.

11
Objects

An object has both a state and behavior. The state


defines the object, and the behavior defines what
the object does.

12
Example: Defining Classes and Creating Objects

TV

TestTV Run

13
Constructor
A constructor with no parameters is referred to as a
no-arg constructor or default constructor.
· Constructors must have the same name as the
class itself.
· Constructors do not have a return type—not
even void.
· Constructors are invoked using the new operator
when an object is created. Constructors play the
role of initializing objects.
14
Default Constructor
A class may be defined without constructors. In
this case, a no-arg constructor with an empty body
is implicitly defined in the class. This constructor,
called a default constructor, is provided
automatically only if no constructors are explicitly
defined in the class.

15
Creating Objects Using Constructors

new ClassName();

Example:
new Circle();

new Circle(5.0);

16
Declaring Object Reference Variables

To reference an object, assign the object to a


reference variable.

To declare a reference variable, use the syntax:

ClassName objectRefVar;

Example:
Circle myCircle;

17
Declaring/Creating Objects in a Single Step

ClassName objectRefVar = new ClassName();

Assign object reference Create an object


Example:
Circle myCircle = new Circle();

18
Accessing Object’s Members

❑ Referencing the object’s data:


objectRefVar.data
e.g., myCircle.radius

❑ Invoking the object’s method:


objectRefVar.methodName(arguments)
e.g., myCircle.getArea()

19
animation
Trace Code
Declare myCircle

Circle myCircle = new Circle(5.0); no value


myCircle
Circle yourCircle = new Circle();

yourCircle.radius = 100;

20
animation
Trace Code

Circle myCircle = new Circle(5.0); no value


myCircle
Circle yourCircle = new Circle();

yourCircle.radius = 100;

Create a circle

21
animation
Trace Code

Circle myCircle = new Circle(5.0);


myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100; Assign object reference


to myCircle

22
animation
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100;

yourCircle no value

Declare yourCircle

23
animation
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100;

yourCircle no value

Create a new
Circle object

24
animation
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100;

yourCircle reference value

Assign object reference


to yourCircle

25
animation
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();

yourCircle.radius = 100;

yourCircle reference value

Change radius in
yourCircle

26
Reference Data Fields
The data fields can be of reference types. For example,
the following Student class contains a data field name of
the String type.
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value false
char gender; // c has default value '\u0000'
}

27
The null Value

If a data field of a reference type does not


reference any object, the data field holds a
special literal value, null.

28
Default Value for a Data Field
The default value of a data field is null for a
reference type, 0 for a numeric type, false for a
boolean type, and '\u0000' for a char type.

public class Test {


public static void main(String[] args) {
Student student = new Student();
System.out.println("name? " + student.name);
System.out.println("age? " + student.age);
System.out.println("isScienceMajor? " + student.isScienceMajor);
System.out.println("gender? " + student.gender);
}
}

29
Differences between Variables of
Primitive Data Types and Object Types

30
Copying Variables of Primitive Data Types and
Object Types

31
Static

A way of sharing variables, constants, and


methods. We use ONE static Math class, like a
library.

Static variables are shared by all the instances


of the class.
Static methods are not tied to a specific object.
Static constants are final variables shared by all
the instances of the class.
32
Static Variables, Constants,
and Methods

To declare static variables, constants, and methods,


use the static modifier.

33
Objects: Instance
Variables, and Methods

Instance variables belong to a specific object.

Instance methods are invoked by an instance of the


class.

Instance variables and methods are specified by


omitting the static keyword.

34
Objects: Instance
Variables, and Methods

35
Static Variables, Constants,
and Methods

Object Oriented Programming 36


Static

numberOfObjects is shared by ALL


objects because it is specified as static;
whereas, radius is an instance variable
that is only used in the instance of an object
to store the value for that object.

37
Static

Object Oriented Programming 38


Static Variables, Constants,
and Methods

• The radius in circle1 is independent of the


radius in circle2 and is stored in a different
memory location.
• Changes made to circle1’s radius do not affect
circle2’s radius,and vice versa.
Object Oriented Programming 39
Static Variables, Constants,
and Methods

• If you want all the instances of a class to share data, use static
variables, also known as class variables.
• A static variable is shared by all objects of the same class. A
static method cannot access instance members of the class.
• Static variables store values for the variables in a common
memory location.
• Because of this common location, if one object changes the
value of a static variable, all objects of the same class are
affected.
• Java supports static methods as well as static variables. Static
methods can be called without creating an instance of the class.

Object Oriented Programming 40


Array of Objects

Circle[] circleArray = new Circle[10];

An array of objects is actually an array of


reference variables. So invoking
circleArray[1].getArea() involves two
levels of referencing as shown in the next
figure. circleArray references to the
entire array. circleArray[1] references to a
Circle object.
41
Array of Objects

Circle[] circleArray = new Circle[10];

42
Array of Objects

• An array can hold objects as well as primitive type


values.
Circle[] circleArray = new Circle[10];
• To initialize circleArray, you can use a for loop
like this one:
for (int i = 0; i < circleArray.length; i++) {
circleArray[i] = new Circle(); }
• An array of objects is actually an array of
reference variables.

Object Oriented Programming 43


Array of Objects

• Person mary = new Person ( );


• int myArray[ ] = new int[5];
• int myArray[ ] = {1, 4, 9, 16, 25};
• String languages [ ] = {"Prolog",
"Java"};

44
Getters et Setters
Getters et Setters (Accesseurs et mutateurs)
Getters and Setters

Accessors (getters) are methods that will allow us to display


the variables of our objects.

mutators (setters) are methods that will allow us to modify


the variables of our objects.
Getters et Setters (Accesseurs et mutateurs)
Getters and Setters
Getters et Setters (Accesseurs et mutateurs)
Getters and Setters
Getters et Setters (Accesseurs et mutateurs)
Getters and Setters
Getters et Setters (Accesseurs et mutateurs)
Getters and Setters

These methods (accessors and mutators) must obviously be


public in order to be accessible from other classes (otherwise
there would be no point).

A mutator is always of type void, because it returns nothing.


An accessor returns a variable and will therefore be of its
type.
Mot clé « This »
Keyword: this
• The “This” clause is typically used with getters and setters.
It provides access to the attributes of the current class.
It means “This class”.
La surcharge (overloading)
Overloading
Overloading occurs when two or more methods in a class have
the same method name but different parameters
La surcharge (overloading)
Overloading
La surcharge (overloading)
Overloading

You might also like