Week 06&07_Classes and Objects
Week 06&07_Classes and Objects
Baby
Object oriented programming
• Represent the real world
Baby
Name
Sex
Weight
Object Oriented Programming
• Objects group together
– Primitives (int, double, char, etc..)
– Objects (String, etc…)
Baby
String name
boolean isMale
double weight
Why use classes?
• Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
Why use classes?
• Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
// little baby david
String nameDavid2; David2?
double weightDavid2; Terrible ®
Why use classes?
• Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
// little baby david
String nameDavid2; David2?
double weightDavid2; Terrible ®
Name
Weight
Sex
…
Baby1
Why use classes?
496 more
Babies …
Baby1 Baby2 Baby3 Baby4
Nursery
Why use classes?
More nurses…
496 more
Babies …
Baby1 Baby2 Baby3 Baby4
Nursery
Why use classes?
[]
Nurse
[]
Baby
Nursery
Why use classes?
[]
Nurse
[] ER
Baby
Nursery
Hospital
Defining classes
Class - overview
public class Baby {
String name;
boolean isMale;
double weight;
int numPoops = 0;
Class
void poop() {
Definition
numPoops += 1;
System.out.println(“Dear mother, ”+
“I have pooped. Ready the diaper.”);
}
}
Class - overview
}
Let’s declare a baby!
public class Baby {
fields
methods
}
Note
• Class names are Capitalized
• 1 Class = 1 file
TYPE var_name;
TYPE var_name = some_value;
}
Baby fields
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
}
Baby Siblings?
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
XXXXX YYYYY;
}
Baby Siblings?
public class Baby {
String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
Baby[] siblings;
}
Ok, let’s make this baby!
CLASSNAME ([ARGUMENTS]) {
}
}
// class Instances
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”, true);
Baby knox = new Baby(“Knox Jolie-Pitt”, true);
Accessing fields
• Object.FIELDNAME
Object
How java stores objects
• Objects are too big to fit in a variable
– Stored somewhere else
– Variable stores a number that locates the object
Object’s
location Object Object Object
no
References
Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Name=“shiloh”
shiloh1 shiloh2
References
Baby mybaby = new Baby(“davy”, true)
mybaby.name = “david”
baby1 = baby2
baby2 baby2
location location
baby1 baby2
object object
baby1 baby2
References
• Using = updates the reference.
baby1 = baby2
baby2
location
baby1 baby2
object object
baby1 baby2
References
• using [ ] or
– Follows the reference to the object
– May modify the object, but never the reference
• Imagine
– Following directions to a house
– Moving the furniture around
• Analogous to
– Following the reference to an object
– Changing fields in the object
Methods and references
Static void doSomething(int x, int[] ys, Baby b) {
x = 99;
ys[0] = 99;
b.name = “99”;
}
...// in main
int i = 0;
int[] j = {0};
Baby k = new Baby(“50”, true);
doSomething(i, j, k);
What is
b1.numBabiesMade?
b2.numBabiesMade?
static example
• Keep track of the number of babies that have
been made.