Lec03 Class Relationships
Lec03 Class Relationships
Advanced Programming
1
Last Lecture
• Program development
• Identifying classes and objects
• Sequence diagrams
• Working with objects
o Objects as parameters
o Variable reassignment
o Instance variables
2
This Lecture
• Class relationships
3
UML: Quick Introduction
• UML stands for the Unified Modeling Language
o We will cover this in depth in later lectures
Player Die
Attributes Attributes
Methods Methods
© Vivek Kumar
5
Class Relationships
• The whole point of OOP is that your code
replicates real world objects, thus making your
code readable and maintainable
• When we say real world, the real world has
relationships
• When writing a program, need to keep in mind
“big picture”—how are different classes related to
each other?
© Vivek Kumar
6
Most Common Class Relationships
• Composition
o A “contains” B
• Association
o A “knows-about” B
• Dependency
o A “depends on” B
• Inheritance
o HarleyDavidson “is-a” Bike
© Vivek Kumar
7
Composition Relationship
• Class A contains object of class B
o A instantiate B
• Thus A knows about B and can call methods on it
• But this is not symmetrical! B can’t automatically call
methods on A
• Lifetime?
o The death relationship
o Garbage collection of A means B also gets garbage collected
© Vivek Kumar
8
Composition in UML
• Represented by a solid arrow with diamond head
• In below UML diagram, A is composed of B
A B
Attributes Attributes
Methods Methods
© Vivek Kumar
9
Composition Example (1/2)
class Project {
private String name;
• Manager is fixed for a public boolean status() { ... }
12
Association in UML
• Represented by a solid arrow
• In below UML diagram, A holds a reference of B
A B
Attributes Attributes
Methods Methods
© Vivek Kumar
13
Association Example (1/4)
class Project {
private String name;
• A contractor’s project keep’s public boolean status() { ... }
changing as per company’s .....
}
policy and contractor’s // Contractor’s project keep
performance changing
class Contractor {
private Project currentProject;
public Contractor(Project proj) {
this.currentProject = proj;
Contractor Project
}
currentProject name public void setProject(Project
setProject() status() proj){
this.currentProject = proj;
}
14
© Vivek Kumar }
Associations Example (2/4)
15
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
● As noted, PetShop contains a
public class DogGroomer {
DogGroomer, so it can send
messages to the DogGroomer
● But what if the DogGroomer public DogGroomer() {
needs to send messages to the // this is the constructor!
PetShop she works in? }
17
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
public class DogGroomer {
18
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
public class DogGroomer {
19
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
public class DogGroomer {
20
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
● Now store myPetShop in
public class DogGroomer {
instance variable _petShop
● _petShop now points to same private PetShop _petShop;
23
Andries van Dam © 2016 9/22/16
Association: Under the Hood (2/5)
public class PetShop { public class DogGroomer {
private DogGroomer _groomer; private PetShop _petShop;
Somewhere else in our code, someone calls new PetShop(). An instance of PetShop is created somewhere in
memory and PetShop’s constructor initializes all its instance variables (just a DogGroomer here) 24
Andries van Dam © 2016 9/22/16
Association: Under the Hood (3/5)
public class PetShop { public class DogGroomer {
private DogGroomer _groomer; private PetShop _petShop;
The PetShop instantiates a new DogGroomer, passing itself in as an argument to the DogGroomer’s constructor
(remember the this keyword?) 25
Andries van Dam © 2016 9/22/16
Association: Under the Hood (4/5)
public class PetShop { public class DogGroomer {
private DogGroomer _groomer; private PetShop _petShop;
When the DogGroomer’s constructor is called, its parameter, myPetShop, points to the same PetShop that was
passed in as an argument. 26
Andries van Dam © 2016 9/22/16
Association: Under the Hood (5/5)
public class PetShop { public class DogGroomer {
private DogGroomer _groomer; private PetShop _petShop;
The DogGroomer sets its _petShop instance variable to point to the same PetShop it received as an argument.
Now it “knows about” the petShop that instantiated it! And therefore so do all its methods... 27
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
● Here we have the class public class Professor {
28
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
● The Professor needs to know public class Professor {
29
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
● Here’s our solution! public class Professor {
private TA _ta1;
● Remember, you can choose private TA _ta2;
private TA _ta3;
your own names for the private TA _ta4;
instance variables and public Professor(TA firstTA,
parameters TA secondTA, TA thirdTA
TA fourthTA) {
● The Professor can now send a _ta1 = firstTA;
message to one of his TAs like _ta2 = secondTA;
_ta3 = thirdTA;
this: _ta4 = fourthTA;
}
_ta1._runRefresherModule(
/* additional methods elided */
); }
30
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
public class Course {
● We’ve got the Professor
// declare Professor instance var.
class down // declare four TA instance vars.
// …
● Now let’s create a professor // …
and TAs from a class that // …
31
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
● We declare _vivek,
public class Course {
_akanksha, _akash,
_alind and _abhiprayah private Professor _vivek;
private TA _akanksha;
as instance variables private TA _akash;
private TA _alind;
● In the constructor, we private TA _abhiprayah;
instantiate them public Course() {
_akanksha = new TA();
● Since the constructor of _akash = new TA();
Professor takes in 4 TAs, _alind = new TA();
_abhiprayah = new TA();
we pass in _akanksha, _vivek = new Professor(_akanksha,
_akash, _alind and _abhiprayah);
_akash , _alind ,
_abhiprayah }
}
32
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
public class Professor { public class Course {
}
}
34
Andries van Dam © 2016 9/22/16
Associations Example (4/4)
● This doesn’t work: when we
public class Course {
instantiate _akanksha,
_akash, _alind and private
private
Professor _vivek;
TA _akanksha;
_abhiprayah, we would private TA _akash;
private TA _alind;
like to pass them an private TA _abhiprayah;
argument, _vivek public Course() {
_akanksha = new TA();
● But _vivek hasn’t been _akash = new TA();
_alind = new TA();
instantiated yet! And can’t _abhiprayah = new TA();
_vivek = new Professor(_akanksha,
initialize _vivek first _akash , _alind ,
because the TAs haven’t _abhiprayah);
}
been created yet… }
36
Andries van Dam © 2016 9/22/16
Associations Example (4/4)
public class Course {
public class TA { private Professor _vivek;
private TA _akanksha;
private Professor _professor; private TA _akash;
private TA _alind;
public TA() { private TA _abhiprayah;
//Other code elided public Course() {
_akanksha = new TA();
} _akash = new TA();
_alind = new TA();
public void setProf(Professor prof) { _abhiprayah = new TA();
_professor = prof; _vivek = new Professor(_akanksha,
} _akash , _alind ,
} _abhiprayah);
_akanksha.setProf(_vivek);
● Now each TA will know about _akash.setProf(_vivek);
_alind.setProf(_vivek);
_vivek! _abhiprayah.setProf(_vivek);
} 37
Andries van Dam © 2016 9/22/16
Question
38
Andries van Dam © 2016 9/22/16
Dependency
• Class A depends on class B if A cannot carry out its
work without B, but B is neither a component of A nor it
has association with A
• A is requesting service from an object of class B
o A or B “doesn’t know” about each other (no association)
o A or B “doesn’t contain” each other (no composition)
• But this is not symmetrical! B doesn’t depends on A
© Vivek Kumar
39
Dependency in UML
• Represented by a dashed arrow starting from
the dependent class to its dependency
o A is dependent on B
o A is requesting service from B
A B
Attributes Attributes
Methods Methods
© Vivek Kumar
40
Dependency Example (1/3)
class Die {
private int faceValue, faces;
.....
Player Die public void roll() { ..... }
}
faceValue
class Player {
faces
public void takeTurn(Die die) {
takeTurn() roll() die.roll();
}
}
© Vivek Kumar
41
Dependency Example (2/3)
class Product {
private double price;
.....
public double getPrice()
Cart Product { ..... }
cartPrice price }
addProduct getPrice()
class Cart {
private double cartPrice;
public void addProduct(Product p)
{
cartPrice += p.getPrice();
}
}
© Vivek Kumar
42
Dependency Example (3/3)
class Course {
private String name;
.....
public String getName() { ..... }
CourseSchedule Course }
name
total class CourseSchedule {
courses private int total;
add() getName() private String courses[];
drop() public void addCourse(Course c) {
courses[total++] =
c.getName();
}
.....
}
© Vivek Kumar
43
Next Lecture
• Inheritance in Java
44