ITI 1121. Introduction to Computing II – subtitle
ITI 1121. Introduction to Computing II
Object oriented programming: visibility, variables and class methods
by
Marcel Turcotte
Version January 20, 2020
Preamble
Preamble
Overview
Overview
Object oriented programming: visibility, variables and class methods
We discover the role of the visibility modifiers in order to promote encapsulation. We add
to our knowledge the concepts of class variables and class methods. Finally, we will see
the role of a pre-defined reference variable this.
General objective :
This week, you will be able to compare the concepts of instance and class variables,
as well as the instance methods and class methods.
1 51
Preamble
Learning objectives
Learning objectives
Describe the Java mechanisms to promote encapsulation
Explain in your own words the following concepts: instance and class variables,
instance and class methods.
Write simple Java programs based on object-oriented programming.
Lectures:
Pages 573-579 from E. Koffman and P. Wolfgang.
2 51
Lectures (continued):
Basic information
docs.oracle.com/javase/tutorial/java/concepts/object.html
docs.oracle.com/javase/tutorial/java/concepts/class.html
Detailed information
docs.oracle.com/javase/tutorial/java/javaOO/classes.html
docs.oracle.com/javase/tutorial/java/javaOO/objects.html
docs.oracle.com/javase/tutorial/java/javaOO/more.html
docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Exercises
docs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html
docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html
3 51
https://docs.oracle.com/javase/tutorial/java/concepts/object.html
https://docs.oracle.com/javase/tutorial/java/concepts/class.html
https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
https://docs.oracle.com/javase/tutorial/java/javaOO/more.html
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
https://docs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html
https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html
Preamble
Plan
Plan
1 Preamble
2 Encapsulation
3 Static context
4 [Pleaseinsertintopreamble] What is this? [Pleaseinsertintopreamble]
5 Final
6 Prologue
4 51
Encapsulation
Definition: encapsulation
In object-oriented programming, encapsulation consists of grouping together in one unit
(the object) the data and the methods that manipulate them.
5 51
Java: visibility modifiers
In Java, visibility modifiers allow us to control the access of variables and methods.
pub l i c c l a s s Po in t {
p r i v a t e i n t x , y ;
pub l i c i n t getX ( ) {
re tu rn x ;
}
pub l i c vo id setX ( i n t v a l u e ) {
x = v a l u e ;
}
}
6 51
Java: visibility modifiers
pub l i c c l a s s Po in t {
p r i v a t e i n t x , y ;
pub l i c i n t getX ( ) { re tu rn x ; }
pub l i c i n t getY ( ) { re tu rn y ; }
pub l i c boolean e q u a l s ( Po in t o t h e r ) {
i f ( o t h e r == nu l l ) {
re tu rn f a l s e ;
} e l s e {
re tu rn x == o t h e r . getX ( ) && y == o t h e r . getY ( ) ;
}
}
}
7 51
Java: visibility modifiers
Is the declaration of the method equals valid?
pub l i c c l a s s Po in t {
p r i v a t e i n t x , y ;
pub l i c i n t getX ( ) { re tu rn x ; }
pub l i c i n t getY ( ) { re tu rn y ; }
pub l i c boolean e q u a l s ( Po in t o t h e r ) {
i f ( o t h e r == nu l l ) {
re tu rn f a l s e ;
} e l s e {
re tu rn x == o t h e r . x && y == o t h e r . y ;
}
}
}
8 51
Example : encapsulation
pub l i c c l a s s Test {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
Po in t p ;
p = new Po in t ( ) ;
p . x = 4 ;
}
}
javac Test.java
Test.java:5: error: x has private access in Point
p.x = 4;
^
1 error
9 51
Java: visibility modifiers
In Java, visibility modifiers allow us to control the access of variables and methods.
A variable or method whose visibility is private is only accessible in the body of the
class where it is defined.
A variable or method whose visibility is public is accessible in the body of the class
where it is defined, but also in all the other classes of the program.
In ITI1121, instance variables should always be declared with visibility private!
10 51
Discussion: encapsulation
Carefully consider the following two implementations of the class Pair.
Can one implementation replace the other without causing any compile or runtime
errors for the other classes of an application?
11 51
pub l i c c l a s s P a i r {
p r i v a t e i n t f i r s t ;
p r i v a t e i n t second ;
pub l i c P a i r ( i n t f i r s t I n i t , i n t s e c o n d I n i t ) {
f i r s t = f i r s t I n i t ;
second = s e c o n d I n i t ;
}
pub l i c i n t g e t F i r s t ( ) {
re tu rn f i r s t ;
}
pub l i c i n t getSecond ( ) {
re tu rn second ;
}
pub l i c vo id s e t F i r s t ( i n t v a l u e ) {
f i r s t = v a l u e ;
}
pub l i c vo id se tSecond ( i n t v a l u e ) {
second = v a l u e ;
}
}
pub l i c c l a s s P a i r {
p r i v a t e i n t [ ] e l ems ;
pub l i c P a i r ( i n t f i r s t , i n t second ) {
e lems = new i n t [ 2 ] ;
e l ems [ 0 ] = f i r s t ;
e l ems [ 1 ] = second ;
}
pub l i c i n t g e t F i r s t ( ) {
re tu rn e lems [ 0 ] ;
}
pub l i c i n t getSecond ( ) {
re tu rn e lems [ 1 ] ;
}
pub l i c vo id s e t F i r s t ( i n t v a l u e ) {
e lems [ 0 ] = v a l u e ;
}
pub l i c vo id se tSecond ( i n t v a l u e ) {
e lems [1]= v a l u e ;
}
}
Discussion: encapsulation
Can we replace one implementation with the other without causing compilation or
execution errors for the following statements?
P a i r p ;
p = new P a i r (2 , 4 ) ;
System . out . p r i n t l n ( p . g e t F i r s t ( ) ) ;
p . s e tSecond ( 1 2 ) ;
14 51
Discussion: encapsulation (take 2)
pub l i c c l a s s Po in t {
p r i v a t e i n t x
p r i v a t e i n t y ;
pub l i c vo id setX ( i n t v a l u e ) {
i f ( v a l u e < 0 | | v a l u e > 1024) {
x = 0 ;
} e l s e {
x = v a l u e ;
}
}
}
15 51
Discussion: private methods
Can you imagine situations for which one would like to declare a method private?
16 51
pub l i c c l a s s Po in t {
p r i v a t e i n t x
p r i v a t e i n t y ;
p r i v a t e boolean i s V a l i d ( i n t v a l u e ) {
i f ( v a l u e < 0 | | v a l u e > 1024) {
re tu rn t rue ;
} e l s e {
re tu rn f a l s e ;
}
}
pub l i c vo id setX ( i n t v a l u e ) {
i f ( i s V a l i d ( v a l u e ) ) {
x = v a l u e ;
} e l s e {
x = 0 ;
}
}
}
Discussion: interface
The “interface” of a class is made up of public methods
Methods that should not be part of the interface will be declared “private”
18 51
Static context
Definition: class variable
A class variable is a variable defined in the body of the class and that is shared by the
instances (objects) of this class (one memory location is used by all the instances of the
class).
pub l i c c l a s s Po in t {
pub l i c s t a t i c i n t MAX_VALUE = 100 ;
p r i v a t e i n t x
p r i v a t e i n t y ;
pub l i c vo id moveRight ( ) {
x = x + 1 ;
i f ( x > MAX_VALUE) {
x = 0 ;
}
}
}
19 51
Definition: class variable
The keyword static introduces a class variable. As you can see, you have to make an
effort to create a class variable, this is not what you get by default.
pub l i c c l a s s Po in t {
pub l i c s t a t i c i n t MAX_VALUE = 100 ;
p r i v a t e i n t x
p r i v a t e i n t y ;
pub l i c vo id moveRight ( ) {
x = x + 1 ;
i f ( x > MAX_VALUE) {
x = 0 ;
}
}
}
20 51
Class variable
The declaration of a constant is a good example of a situation where class variables
are useful.
Can you find more examples where class variables could be useful?
21 51
pub l i c c l a s s Ticke t {
p r i v a t e s t a t i c i n t l a s t = 100 ;
p r i v a t e i n t number ;
pub l i c Ticke t ( ) {
number = l a s t ;
l a s t=l a s t +1;
}
pub l i c i n t ge tSe r i a lNumbe r ( ) {
re tu rn number ;
}
}
22 51
pub l i c c l a s s Ticke t {
p r i v a t e s t a t i c i n t l a s t = 100 ;
p r i v a t e i n t number ;
pub l i c Ticke t ( ) {
number = l a s t ;
l a s t=l a s t +1;
}
pub l i c i n t ge tSe r i a lNumbe r ( ) {
re tu rn number ;
}
}
23 51
Definition: class method
Class (static) methods do not belong to an object in particular. They are therefore
shared by the instances of the class. Since they are not associated with an object, they
do not have access to instance variables.
24 51
Class method
Does the method isValid use any instance variable?
pub l i c c l a s s Po in t {
p r i v a t e i n t x
p r i v a t e i n t y ;
p r i v a t e boolean i s V a l i d ( i n t v a l u e ) {
i f ( v a l u e < 0 | | v a l u e > 1024) {
re tu rn t rue ;
} e l s e {
re tu rn f a l s e ;
}
}
}
25 51
Class method
The method isValid should be a static (class) method.
pub l i c c l a s s Po in t {
p r i v a t e i n t x
p r i v a t e i n t y ;
p r i v a t e s t a t i c boolean i s V a l i d ( i n t v a l u e ) {
i f ( v a l u e < 0 | | v a l u e > 1024) {
re tu rn t rue ;
} e l s e {
re tu rn f a l s e ;
}
}
}
26 51
Class method
pub l i c c l a s s Po in t {
p r i v a t e i n t x
p r i v a t e i n t y ;
pub l i c s t a t i c i n t compareTo ( i n t a , i n t b ) {
i n t r e s u l t ;
i f ( a < b ) {
r e s u l t = −1;
} e l s e i f ( a == b ) {
r e s u l t = 0 ;
} e l s e {
r e s u l t = 1 ;
}
re tu rn r e s u l t ;
}
}
27 51
Examples of static methods
http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
http://docs.oracle.com/javase/8/docs/api/java/lang/System.html
28 51
http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
http://docs.oracle.com/javase/8/docs/api/java/lang/System.html
Calling a static method (static context)
Use the name of the class followed by the name of the method for calling a class
method.
Use the class name followed by the variable name to access a class variable.
double a ;
a = Math . abs ( −1 .6) ;
a = Math . max (1024 . 0 , Double .MAX_VALUE) ;
a = Math . random ( ) ;
a = Math . s q r t ( Double .MAX_VALUE) ;
a = Math . pow ( 2 . 0 , 3 2 . 0 ) ;
29 51
« What is this? »
« What is this? »
Each object has a reference called this. It designates the object itself.
pub l i c c l a s s BankAccount {
p r i v a t e double ba l ance ;
// . . .
pub l i c boolean t r a n s f e r ( BankAccount other , double a ) {
i f ( t h i s == o t h e r ) {
re tu rn f a l s e ;
}
. . .
}
}
30 51
45,000,000
angelina
balance
transfer( )
angelina.transfer( brad, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
args
...
31 51
45,000,000
angelina
balance
transfer( )
angelina.transfer( brad, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
this
Activation Frame
for transfer
args
...
other
a
32 51
45,000,000
angelina
balance
transfer( )
angelina.transfer( brad, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
this
Activation Frame
for transfer
args
...
other
100a
33 51
45,000,000
angelina
balance
transfer( )
brad.transfer( angelina, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
args
...
34 51
45,000,000
angelina
balance
transfer( )
brad.transfer( angelina, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
this
Activation Frame
for transfer
args
...
other
a
35 51
45,000,000
angelina
balance
transfer( )
brad.transfer( angelina, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
this
Activation Frame
for transfer
args
...
other
100a
36 51
45,000,000
angelina
balance
transfer( )
angelina.transfer( angelina, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
args
...
37 51
45,000,000
angelina
balance
transfer( )
angelina.transfer( angelina, 100 )
100,000,000
brad
balance
transfer( )
Activation Frame
for main
this
Activation Frame
for transfer
args
...
other
100a
38 51
« What is this »
pub l i c c l a s s Date {
p r i v a t e i n t day ;
p r i v a t e i n t month ;
pub l i c Date ( i n t day , i n t month ) {
t h i s . day = day ;
t h i s . month = month ;
}
// . . .
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
Date d ;
i n t day , month ;
day = 17 ; month = 1 ;
d = new Date ( day , month ) ;
}
}
39 51
d
1
17
Activation Frame
(working memory)
for main
args
...
month
day
⇒ Working memory for the method main
40 51
d
day
1
17
Activation Frame
(working memory)
for main
args
...
month
day
month
⇒ Executing “new Date(day,month)”, creating an object
41 51
d
day
1
17
Activation Frame
(working memory)
for main
this
Activation Frame
(working memory)
for transfer
args
...
day
month
month
day
month
⇒ Calling the constructor, allocating the working memory for the call
42 51
d
day
1
17
1
Activation Frame
(working memory)
for main
this
Activation Frame
(working memory)
for transfer
args
...
17day
month
month
day
month
⇒ Copying the values of the actual parameters to the formal parameters
43 51
17
d
day
1
17
1
Activation Frame
(working memory)
for main
this
Activation Frame
(working memory)
for transfer
args
...
17day
month
month
day
1
month
⇒ Executing the body of the constructor, copying the value of day to this.day, month
to this.month
44 51
17
d
day
1
17
Activation Frame
(working memory)
for main
args
...
month
day
1
month
⇒ The execution of the constructor terminates, its associated working memory is
destroyed, assigning the reference of the object to d
45 51
« What is this? »
pub l i c c l a s s Date {
p r i v a t e i n t day ;
p r i v a t e i n t month ;
pub l i c Date ( i n t day , i n t month ) {
t h i s . day = day ;
t h i s . month = month ;
}
// . . .
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
Date d ;
i n t day , month ;
day = 17 ; month = 1 ;
d = new Date ( day , month ) ;
}
}
⇒ this is used to lift (remove) an ambiguity, the parameter day and the instance variable
day.
46 51
Final
Java: final
The keyword final implies that the value of the variable cannot be changed.
pub l i c c l a s s Po in t {
pub l i c s t a t i c f i n a l i n t MAX_VALUE = 100 ;
p r i v a t e i n t x
p r i v a t e i n t y ;
pub l i c vo id moveRight ( ) {
x = x + 1 ;
i f ( x > MAX_VALUE) {
x =0;
}
}
}
47 51
Prologue
Summary
Encapsulation is about putting the data and the operations that transform them
into a single unit (the object)
visibility modifiers support encapsulation
Static variables and methods are shared by the instances of the class, and all other
classes if their visibility is public
Each object has a reference this. It designates the object itself.
48 51
Next module
Interface
49 51
References I
E. B. Koffman and Wolfgang P. A. T.
Data Structures: Abstraction and Design Using Java.
John Wiley & Sons, 3e edition, 2016.
50 51
Marcel Turcotte
Marcel.
School of Electrical Engineering and Computer Science (EECS)
University of Ottawa
51 / 51
Marcel.
Preamble
Overview
Learning objectives
Plan
Encapsulation
Static context
« What is this? »
Final
Prologue