object oriented programming in java · object oriented programming in java mikhail markov...

25
OBJECT ORIENTED P ROGRAMMING IN JAVA MIKHAIL MARKOV DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY SFWRENG/COMPSCI 2S03 (FALL 2015 ) TUTORIAL 5

Upload: others

Post on 21-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

OBJECT ORIENTED PROGRAMMING IN JAVA

MIKHAIL MARKOV

DEPARTMENT OF COMPUTING AND SOFTWAREMCMASTER UNIVERSITY

SFWRENG/COMPSCI 2S03 (FALL 2015)

TUTORIAL 5

Page 2: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Contacts

• Graduate Teaching Assistants:

Akhil Krishnan: [email protected]

Michael Liut: [email protected]

Mikhail Markov: [email protected]

• Undergraduate Teaching Assistants:

Jemar Jones: [email protected]

Wenqiang Chen: [email protected]

Page 3: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Outline

• Class and Object

• Class and Object: example

• Class bird example: class definition

• Class bird example: objects

• Class bird example: class definition with constructors

• Class bird example: objects with constructors

• Encapsulation

• Encapsulation: example

• Immutability

• Immutability: example of changing the values

• Immutability: the same example with the “final” keyword

• Method Overloading

• Method Overloading: example

Page 4: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class and Object

• Class - the “blue-print” that describes the possible behaviours and states that object of its type support.

• Object - an instance of the class (the houses built from the blue-print). Have specific values of the states and behaviours.

Page 5: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class and Object: example

Example

• Class: bird

• State: size, colour, family, etc.

• Behavior: flying, swimming, eating, etc.

• Objects: sparrow, duck, goose, etc.

• Object_Sparrow States: size = small, colour = brown-grey, family = Passeridae; Sparrow Behavior: flying

Page 6: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: class definition

Page 7: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: objects

The result:

Page 8: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: objects

We can manually assign the values of the states to every object.

But what if we have hundreds of the similar objects?? Can we do better?

Page 9: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: objects

Yes we can!

We need to use constructors!

With multiple constructors we can create the objects with all or just several same states.

Page 10: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: class definition with constructors

Page 11: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: objects with constructors

Page 12: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Class bird example: objects with constructors

The result:

Page 13: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Encapsulation

What will happen if we’ll change the modifier of the class states from “public” to “private” and we’ll try to assign the values of the states from the main function?

Page 14: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Encapsulation

The result is …

Page 15: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Encapsulation

The result is an exception (doesn’t compile) as the states of the class with the modifier “private” becomes not visible. So it can’t be changed somehow from another class.

Page 16: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Encapsulation

The benefit of encapsulation is that we can, for instance, define the possible values of the states. This values can’t be changed to another (not in our list) like it’s possible with the “public” modifier.

Let’s look at the example.

Page 17: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Encapsulation: example

Page 18: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Encapsulation: example

The result:

Page 19: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Immutability

In general we can change the values of the states multiple times.

Let’s look at the example.

Page 20: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Immutability: example of changing the values

Page 21: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Immutability: example of changing the values

The result:

Page 22: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Immutability: the same example with the “final” keyword

Let’s add “final” keyword to add immutability and run the same code.

The result is an exception:

But if we’ll comment the change:The code works:

Page 23: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Method Overloading

Method overloading in used when several methods of the same class have the same name but different number or type of the parameters.

Page 24: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Method Overloading: example

Page 25: OBJECT ORIENTED PROGRAMMING IN JAVA · object oriented programming in java mikhail markov department of computing and software mcmaster university sfwreng/compsci 2s03 (fall 2015)

Method Overloading: example

The result: