iti 1121. introduction to computing ii - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. ·...

61
ITI 1121. Introduction to Computing II Object oriented programming: visibility, variables and class methods by Marcel Turcotte Version January 20, 2020

Upload: others

Post on 20-Jan-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

ITI 1121. Introduction to Computing IIObject oriented programming: visibility, variables and class methods

by

Marcel Turcotte

Version January 20, 2020

Page 2: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Preamble

Page 3: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Preamble

Overview

Page 4: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Overview

Object oriented programming: visibility, variables and class methods

We discover the role of the visibility modifiers in order to promote encapsulation. We addto our knowledge the concepts of class variables and class methods. Finally, we will seethe 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

Page 5: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Preamble

Learning objectives

Page 6: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Learning objectives

Describe the Java mechanisms to promote encapsulationExplain 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

Page 7: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Lectures (continued):

Basic informationdocs.oracle.com/javase/tutorial/java/concepts/object.htmldocs.oracle.com/javase/tutorial/java/concepts/class.html

Detailed informationdocs.oracle.com/javase/tutorial/java/javaOO/classes.htmldocs.oracle.com/javase/tutorial/java/javaOO/objects.htmldocs.oracle.com/javase/tutorial/java/javaOO/more.htmldocs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Exercisesdocs.oracle.com/javase/tutorial/java/concepts/QandE/questions.html

docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html

3 51

Page 8: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Preamble

Plan

Page 9: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Plan

1 Preamble

2 Encapsulation

3 Static context

4 [Pleaseinsertintopreamble] What is this? [Pleaseinsertintopreamble]

5 Final

6 Prologue

4 51

Page 10: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Encapsulation

Page 11: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 12: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 13: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 14: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 15: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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.javaTest.java:5: error: x has private access in Point

p.x = 4;^

1 error

9 51

Page 16: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 theclass where it is defined.A variable or method whose visibility is public is accessible in the body of the classwhere 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

Page 17: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Discussion: encapsulation

Carefully consider the following two implementations of the class Pair.Can one implementation replace the other without causing any compile or runtimeerrors for the other classes of an application?

11 51

Page 18: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 ;}

}

Page 19: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 ;}

}

Page 20: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Discussion: encapsulation

Can we replace one implementation with the other without causing compilation orexecution 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

Page 21: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 xp 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

Page 22: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Discussion: private methods

Can you imagine situations for which one would like to declare a method private?

16 51

Page 23: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

pub l i c c l a s s Po in t {

p r i v a t e i n t xp 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 ;

}}

}

Page 24: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Discussion: interface

The "interface" of a class is made up of public methodsMethods that should not be part of the interface will be declared "private"

18 51

Page 25: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Static context

Page 26: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Definition: class variable

A class variable is a variable defined in the body of the class and that is shared by theinstances (objects) of this class (one memory location is used by all the instances of theclass).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 xp 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

Page 27: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Definition: class variable

The keyword static introduces a class variable. As you can see, you have to make aneffort 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 xp 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

Page 28: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Class variable

The declaration of a constant is a good example of a situation where class variablesare useful.Can you find more examples where class variables could be useful?

21 51

Page 29: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 30: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 31: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Definition: class method

Class (static) methods do not belong to an object in particular. They are thereforeshared by the instances of the class. Since they are not associated with an object, theydo not have access to instance variables.

24 51

Page 32: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 xp 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

Page 33: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 xp 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

Page 34: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Class method

pub l i c c l a s s Po in t {

p r i v a t e i n t xp 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

Page 35: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 36: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Calling a static method (static context)

Use the name of the class followed by the name of the method for calling a classmethod.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

Page 37: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

« What is this? »

Page 38: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

« 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

Page 39: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

angelina.transfer( brad, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

args

...

31 51

Page 40: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

angelina.transfer( brad, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

othera

32 51

Page 41: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

angelina.transfer( brad, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

other100a

33 51

Page 42: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

brad.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

args

...

34 51

Page 43: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

brad.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

othera

35 51

Page 44: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

brad.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

other100a

36 51

Page 45: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

angelina.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

args

...

37 51

Page 46: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

45,000,000

angelina

balance

transfer( )

angelina.transfer( angelina, 100 )

100,000,000

brad

balance

transfer( )Activation Frame for main

this

Activation Framefor transfer

args

...

other100a

38 51

Page 47: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

« 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

Page 48: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

d

1

17Activation Frame(working memory)for main

args

...

monthday

⇒ Working memory for the method main40 51

Page 49: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

d

day

1

17Activation Frame(working memory)for main

args

...

monthday

month

⇒ Executing “new Date(day,month)”, creating an object41 51

Page 50: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

d

day

1

17Activation Frame(working memory)for main

this

Activation Frame(working memory)for transfer

args

...

daymonth

monthday

month

⇒ Calling the constructor, allocating the working memory for the call42 51

Page 51: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

d

day

1

17

1

Activation Frame(working memory)for main

this

Activation Frame(working memory)for transfer

args

...

17daymonth

monthday

month

⇒ Copying the values of the actual parameters to the formal parameters43 51

Page 52: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

17

d

day

1

17

1

Activation Frame(working memory)for main

this

Activation Frame(working memory)for transfer

args

...

17daymonth

monthday

1

month

⇒ Executing the body of the constructor, copying the value of day to this.day, monthto this.month

44 51

Page 53: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

17

d

day

1

17Activation Frame(working memory)for main

args

...

monthday

1

month

⇒ The execution of the constructor terminates, its associated working memory isdestroyed, assigning the reference of the object to d

45 51

Page 54: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

« 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 variableday.

46 51

Page 55: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Final

Page 56: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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 xp 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

Page 57: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Prologue

Page 58: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Summary

Encapsulation is about putting the data and the operations that transform theminto a single unit (the object)visibility modifiers support encapsulationStatic variables and methods are shared by the instances of the class, and all otherclasses if their visibility is publicEach object has a reference this. It designates the object itself.

48 51

Page 59: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Next module

Interface

49 51

Page 60: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

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

Page 61: ITI 1121. Introduction to Computing II - subtitleturcotte/teaching/iti-1121/... · 2020. 1. 20. · ITI1121. IntroductiontoComputingII Objectorientedprogramming: visibility, variables

Marcel [email protected]

School of Electrical Engineering and Computer Science (EECS)University of Ottawa

51 / 51