Poo 2
Poo 2
Abstraction /Encapsulation
1
Object Oriented Thinking
8
Classes
9
UML Class Diagram
10
Objects
An object has a unique identity, state, and
behavior.
11
Objects
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
ClassName objectRefVar;
Example:
Circle myCircle;
17
Declaring/Creating Objects in a Single Step
18
Accessing Object’s Members
19
animation
Trace Code
Declare myCircle
yourCircle.radius = 100;
20
animation
Trace Code
yourCircle.radius = 100;
Create a circle
21
animation
Trace Code
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;
25
animation
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
yourCircle.radius = 100;
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
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.
29
Differences between Variables of
Primitive Data Types and Object Types
30
Copying Variables of Primitive Data Types and
Object Types
31
Static
33
Objects: Instance
Variables, and Methods
34
Objects: Instance
Variables, and Methods
35
Static Variables, Constants,
and Methods
37
Static
• 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.
42
Array of Objects
44
Getters et Setters
Getters et Setters (Accesseurs et mutateurs)
Getters and Setters