B2: Methods and Method Calls
CPSC 210
Learning Goals
- Identify the type of an object
- Identify a method call and the object on which it is called
- Identify a method definition
- Draw a call graph starting from a specified method
- (Use IntelliJ to navigate a sequence of method calls)
OO Terminology
Class | Cat |
Object / Instance | Snowy |
Method | meow(3), sleep(), eat() |
Attribute / Field | name, age, weight |
Constructor | Create a new cat object (e.g. Snowy) |
OO Terminology (2)
Class | Dog |
Object / Instance | Chuck |
Method | bark(2), sleep(), eat() |
Attribute / Field | name, age, weight |
Constructor | Create a new dog object (e.g. Chuck) |
Methods and Objects
Clone the B02-SpaceInvadersBase repo if you have not yet done so:
https://github.students.cs.ubc.ca/CPSC210/B02-SpaceInvadersBase
Class Example: Tank
Class
Methods
Attributes/Fields
Constructor
1
2
3
4
(View > Tool Windows > Structure)
Tank Code Example
Tank myTank;
myTank = new Tank(400);
myTank.faceLeft();
myTank.move();
Declare new variable myTank of type Tank (just a reference!)
Assign new Tank object to variable
Call faceLeft method on Tank object
Call move method on Tank object
Variables, Objects, & References
Tank myTank;
- This does not create an object/instance yet, but only a variable holding a reference to a tank
- We need to create an object specifically
myTank = new Tank(400);
Tank object
direction 1
x 400
myTank
- A variable is space to hold one specific type
of thing, like a reference to a tank:
Tank myTank = new Tank(400);
Tank object
direction 1
x 400
myTank = new Tank(99);
- But we can change what's
in it by assigning to it:
Variables, Objects, & References (2)
myTank
- A variable is space to hold one specific type
of thing, like a reference to a tank:
Tank myTank = new Tank(400);
Tank object
direction 1
x 400
myTank
myTank = new Tank(99);
- But we can change what's
in it by assigning to it:
Variables, Objects, & References (2)
Tank object
direction 1
x 99
- A reference is an address of an object in memory.
- That's a number like a building's address.
- We draw it as an arrow.
Tank myTank = new Tank(400);
Tank object
direction 1
x 400
myTank
Tank myTank2 = myTank;
- But it's really just a number.
- When we copy it, we get the
same number: not a new object!
myTank2
Variables, Objects, & References (3)
myTank.faceLeft();
myTank
Tank object
direction 1
x 400
Variables, Objects, & References (4)
- Let's call a method with the dot operator:
myTank.faceLeft();
myTank
Tank object
direction -1
x 400
Variables, Objects, & References (4)
- Let's call a method with the dot operator:
myTank.move();
x 400
direction -1
Tank object
myTank
Variables, Objects, & References (5)
- Again: find the object, execute the method on it:
myTank.move();
x 399
direction -1
Tank object
myTank
Variables, Objects, & References (5)
- Again: find the object, execute the method on it:
Call Graph Example
Tank
move
Tank
handleBoundary
Tank.move
GamePanel.drawMissiles
GamePanel.drawMissiles
GamePanel.drawMissiles
GamePanel
drawMissiles
SIGame
getMissiles
GamePanel
drawMissile
Missile
getX
Missile
getY
Lecture Lab
Note: constructors are just methods in call graphs, e.g.
Person
Person
The End - Thank You!
B2: Methods & Method Calls
B2 Methods and Method Calls
By Steven Wolfman
B2 Methods and Method Calls
- 529