Lecture4-Working With Objects-Part1
Lecture4-Working With Objects-Part1
Fateme Moradpour
● Call methods: used on an instance of a class
samBot.turnRight();
myCalculator.add(5, 8);
● Remember the one-to-one correspondence rule: list of arguments must match list of
parameters in number, order, and types
● You can create any class you want and invent any methods and
properties you choose for it!
● Let’s define one for the Dog class public void bark(int numTimes) {
// code for barking goes here
● Let’s also add methods for actions }
all Dogs know how to do like bark, public void eat() {
eat, and wag their tails // code for eating goes here
}
public void wagTail() {
// code for wagging tail goes here
}
Working with Objects: Part 1
} 6
public class Dog {
● In this case, django is the variable, and it stores a newly created instance of Dog
the variable name django is also known as an “identifier”
● The “=” operator assigns the instance of Dog that we created to the variable django. We
say “django gets a new Dog”
● Think of the variable like a box; storing a value or reference is like putting
something into the box
//constructor
● Let’s define a new class PetShop which has a
public PetShop() {
testdjango() method
this.testdjango();
don’t worry if the example seems a bit
}
contrived…
public void testdjango() {
● Whenever someone instantiates a PetShop, its Dog django = new Dog();
constructor is called, which calls testdjango() django.bark(5);
to bark, eat, and wag its tail (see definition of Dog django.wagTail();
Dog }
Working with Objects: Part 1 15
DogGroomer
testGroomer() this.testGroomer();
}
● PetShop’s call to testGroomer()
public void testGroomer() {
instantiates a Dog and a DogGroomer,
Dog django = new Dog();
then calls the DogGroomer to groom of
the Dog DogGroomer groomer = new DogGroomer();
groomer.groom(django);
● First two lines could be in either
}
order
Working with Objects: Part 1 } 16
Instances as Parameters (3/3): Flow of Control
public class App {
0. In App's main, a PetShop is instantiated public static void main(String[] args) {
(thereby calling PetShop's constructor). Then:
0. PetShop petSmart = new PetShop();
public PetShop(){
2. Next, it instantiates a DogGroomer and stores
a reference to it in the variable groomer this.testGroomer();
}
3. The groom method is called on groomer, public void testGroomer() {
passing in django as an argument; the groomer
1. Dog django = new Dog();
will think of it as shaggyDog, a synonym
2. DogGroomer groomer = new PetShop();
3. groomer.groom(django);
• Memory (“system memory” aka RAM, not disk or public static void main(String[] args) {
}
to an instance of a class)
public void testGroomer() {
• Here, two references are stored in memory: one Dog django = new Dog();
to a Dog instance, and one to a DogGroomer DogGroomer groomer = new PetShop();
instance groomer.groom(django);
}
Working with Objects: Part 1 18
}
public class PetShop { public class DogGroomer {
DogGroomer groom = new DogGroomer(); // code that groom the fur of shaggyDog
groom.groom(django); }
} }
}
} }
Dog django = new Dog(); // code that groom the fur of shaggyDog
DogGroomer groom = new DogGroomer(); }
groom.groom(django); }
}
} }
public void testGroomer() { public void groom(Dog shaggyDog) {
Dog django = new Dog(); // code that groom the fur of shaggyDog
DogGroomer groom = new DogGroomer();
}
groom.groom(django);
}
}
} }
public void testGroomer() { public void groom(Dog shaggyDog) {
Dog django = new Dog(); // code that groom the fur of shaggyDog
DogGroomer groom = new DogGroomer();
}
groom.groom(django);
}
}
} }
public void testGroomer() { public void groom(Dog shaggyDog) {
Dog django = new Dog(); // code that groom the fur of shaggyDog
DogGroomer groom = new DogGroomer();
}
groom.groom(django);
}
}
different instance) }
groomer.groom(django);
}
Working with Objects: Part 1 27
}
● When we reassign a variable, we do not declare its type again,
Java remembers from first time
● Can reassign to a brand new instance (like in PetShop) or to an
already existing instance by using its identifier
● Now django and scooby refer to the same Dog, specifically the
one that was originally referenced by scooby
Working with Objects: Part 1 28
public class PetShop {
public PetShop(){
this.testGroomer();
groomer.groom(django);
groomer.groom(django);
29
public class PetShop {
public PetShop(){
this.testGroomer();
groomer.groom(django);
groomer.groom(django);
30
public class PetShop {
public PetShop(){
this.testGroomer();
groomer.groom(django);
groomer.groom(django);
31
public class PetShop {
public PetShop(){
this.testGroomer();
groomer.groom(django);
groomer.groom(django);
32
public class PetShop {
public PetShop(){
this.testGroomer();
groomer.groom(django);
groomer.groom(django);
33
● If an instance referred to by a variable goes out of
scope, we can no longer access it. Because we can’t
access the instance, it gets garbage collected
in garbage collection, the space that the instance took up in
memory is freed and the instance no longer exists
}
Working with Objects: Part 1 37
public class PetShop {
scope of groomer
● That way, as long as the PetShop is in business, we’ll have our DogGroomer on
hand
● Instance variables are declared within a class, not within a single method, and
therefore are accessible from anywhere within the class, unlike local variables –
their scope is the entire class
● Instance variables and local variables are identical in terms of what they can
store—either can store a base type (like an int) or a reference to an instance of
some other class
• All methods in a class can access all its properties, to use them and/or change them
● When you run into one of these (we Dog django = new Dog(); //local var
promise, you will), make sure all instance _groomer.groom(django);
variables have been explicitly
initialized, preferably in the constructor, }
and no variables are initialized as null
} NullPointerException