0% found this document useful (0 votes)
34 views44 pages

Lec03 Class Relationships

The document discusses class relationships in object oriented programming. It covers the most common relationships: composition, association, dependency, and inheritance. It provides examples and UML diagrams to illustrate composition and association relationships.

Uploaded by

Akshat Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views44 pages

Lec03 Class Relationships

The document discusses class relationships in object oriented programming. It covers the most common relationships: composition, association, dependency, and inheritance. It provides examples and UML diagrams to illustrate composition and association relationships.

Uploaded by

Akshat Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

CSE201: Monsoon 2023

Advanced Programming

Lecture 03: Class Relationships


Raghava Mutharaju (Section-A)
CSE Department, IIIT-Delhi
[email protected]

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

Slide acknowledgements: Internet resources + CS15, Brown University

3
UML: Quick Introduction
• UML stands for the Unified Modeling Language
o We will cover this in depth in later lectures

• Much detailed than sequence diagrams


• UML diagrams show relationships among classes and objects
o Lines connecting the classes

• A UML class diagram consists of one or more classes, each


with sections for the class name, attributes (data), and
operations (methods)
© Vivek Kumar
4
A Sample UML Class Diagram

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() { ... }

project and is responsible }


.....

for the timely completion // A manager is fixed for a project


of the project. If manager class Manager {
private Project project;
leaves, project is ruined public Manager() {
this.project = new
Project(“ABC”);
Manager Project
}
project name public boolean projectCompleted()
projectCompleted() status() {
return project.status();
}
10
© Vivek Kumar }
Composition Example (2/2)
● PetShop contains a public class PetShop {
DogGroomer
private DogGroomer _groomer;

● Composition relationship because public PetShop() {


PetShop itself instantiates a _groomer = new DogGroomer();
DogGroomer with this.testGroomer();
}
“new DogGroomer();”
public void testGroomer() {
● Since PetShop created a   Dog django = new Dog();//local
DogGroomer and stored it in an var
_groomer.groom(django);
instance variable, all PetShop’s }
methods “know” about the
}
_groomer and can access it
Andries van Dam © 2016 9/22/16
11
Association Relationship
• Association is a relationship between two objects
• Class A and class B are associated if A “knows about”
B, but B is not a component of A
• But this is not symmetrical! B “doesn’t knows about” A
• Class A holds a class level reference to class B
• Lifetime?
o Objects of class A and B have their own lifetime, i.e., they
can exist without each other

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)

public class DogGroomer {

● Association means that one public DogGroomer() {


object knows about another // this is the constructor!
object that is not one of its }
components
public void groom(Dog shaggyDog) {
shaggyDog.setHairLength(1);
}
}

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? }

o the DogGroomer probably needs public void groom(Dog shaggyDog) {


to know several things about her shaggyDog.setHairLength(1);
PetShop: for example, operating }
hours, grooming supplies in stock, }
customers currently in the shop...
16
Andries van Dam © 2016 9/22/16
Associations Example (2/4)

public class DogGroomer {

● The PetShop keeps track of


such information in its properties
public DogGroomer() {
● Can set up an association so // this is the constructor!
}
that DogGroomer can send her
PetShop messages to retrieve public void groom(Dog shaggyDog) {
information she needs shaggyDog.setHairLength(1);
}
}

17
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
public class DogGroomer {

private PetShop _petShop;


● This is what the full association
looks like public DogGroomer(PetShop myPetShop) {
● Let’s break it down line by line _petShop = myPetShop; // store the
assoc.
● But note we’re not yet making }
use of the association in this
public void groom(Dog shaggyDog) {
fragment shaggyDog.setHairLength(1);
}
}

18
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
public class DogGroomer {

private PetShop _petShop;


● We declare an instance variable
public DogGroomer(PetShop myPetShop) {
named _petShop _petShop = myPetShop; // store the
● We want this variable to record assoc.
}
the instance of PetShop that the
DogGroomer belongs to public void groom(Dog shaggyDog) {
shaggyDog.setHairLength(1);
}
}

19
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
public class DogGroomer {

● Modified DogGroomer’s private PetShop _petShop;

constructor to take in a public DogGroomer(PetShop myPetShop) {

parameter of type PetShop }


_petShop = myPetShop; // store the assoc.

//groom method elided


● Constructor will refer to it by the }

name myPetShop public class PetShop {

● Whenever we instantiate a private DogGroomer _groomer;

DogGroomer, we’ll need to pass public PetShop() {


_groomer = new DogGroomer(this);
it an instance of PetShop as an this.testGroomer();
argument, which is the PetShop }

instance that created the //testGroomer() elided

DogGroomer, hence use this }

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;

PetShop instance passed to its public DogGroomer(PetShop myPetShop) {


constructor _petShop = myPetShop; // store the
assoc.
● After constructor has been }
executed and can no longer
reference myPetShop, any public void groom(Dog shaggyDog) {
DogGroomer method can still shaggyDog.setHairLength(1);
}
access same PetShop instance }
by the name _petShop
21
Andries van Dam © 2016 9/22/16
Associations Example (2/4)
● Let’s say we’ve written an accessor
method and a mutator method in the
PetShop class: public class DogGroomer {
getClosingTime() and
private PetShop _petShop;
setNumCustomers(int private Time _closingTime;
customers)
public DogGroomer(PetShop myPetShop) {
● If the DogGroomer ever needs to _petShop = myPetShop; // store assoc.
know the closing time, or needs to _closingTime =
update the number of customers, myPetShop.getClosingTime();
she can do so by calling _petShop.setNumCustomers(10);
}
o getClosingTime() }
o setNumCustomers(int customers)
22
Andries van Dam © 2016 9/22/16
Association: Under the Hood (1/5)
public class PetShop { public class DogGroomer {
private DogGroomer _groomer; private PetShop _petShop;

public PetShop() { public DogGroomer(PetShop myPetShop) {


_groomer = new DogGroomer(this); _petShop = myPetShop;
this.testGroomer(); }
}
/* groom and other methods elided for this
public void testGroomer() { example */
Dog django = new Dog(); }
_groomer.groom(django);
}
} Somewhere in memory...

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;

public PetShop() { public DogGroomer(PetShop myPetShop) {


_groomer = new DogGroomer(this); _petShop = myPetShop;
this.testGroomer(); }
}
/* groom and other methods elided for this
public void testGroomer() { example */
Dog django = new Dog(); }
_groomer.groom(django);
}
} Somewhere in memory...

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;

public PetShop() { public DogGroomer(PetShop myPetShop) {


_groomer = new DogGroomer(this); _petShop = myPetShop;
this.testGroomer(); }
}
/* groom and other methods elided for this
public void testGroomer() { example */
Dog django = new Dog(); }
_groomer.groom(django);
}
} Somewhere in memory...

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;

public PetShop() { public DogGroomer(PetShop myPetShop) {


_groomer = new DogGroomer(this); _petShop = myPetShop;
this.testGroomer(); }
}
/* groom and other methods elided for this
public void testGroomer() { example */
Dog django = new Dog(); }
_groomer.groom(django);
}
} Somewhere in memory...

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;

public PetShop() { public DogGroomer(PetShop myPetShop) {


_groomer = new DogGroomer(this); _petShop = myPetShop;
this.testGroomer(); }
}
/* groom and other methods elided for this
public void testGroomer() { example */
Dog django = new Dog(); }
_groomer.groom(django);
}
} Somewhere in memory...

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 {

Professor // declare instance variables here


// and here…
// and here…
● We want Professor to know // and here!
about his TAs—he didn’t create public Professor(/* parameters */) {
them or vice versa, hence no
containment – they are peer // initialize instance variables!
// …
objects // …
// …
● Let’s set up associations! }

/* additional methods elided */


}

28
Andries van Dam © 2016 9/22/16
Associations Example (3/4)
● The Professor needs to know public class Professor {

about 4 TAs, all of whom will be // declare instance variables here


// and here…
instances of the class TA // and here…
// and here!
● Once he knows about them, he
public Professor(/* parameters */) {
can call methods of the class
TA on them: remindTA, // initialize instance variables!
// …
runRefresherModule, etc. // …
// …
● Take a minute and try to fill in }
this class /* additional methods elided */
}

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 // …

contains all of them: Course public Course() {


// instantiate the four TAs
● Try and fill in this class! // …
// …
o You can assume that the TA // instantiate the professor!
class takes no parameters in its }
constructor. }

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 {

private TA _ta1; private Professor _vivek;


private TA _ta2; private TA _akanksha;
private TA _ta3; private TA _akash;
private TA _ta4; private TA _alind;
private TA _abhiprayah;
public Professor(TA firstTA,
TA secondTA, TA thirdTA public Course() {
TA fourthTA){ _akanksha = new TA();
_akash = new TA();
_ta1 = firstTA; _alind = new TA();
_ta2 = secondTA; _abhiprayah = new TA();
_ta3 = thirdTA; _vivek = new Professor(_akanksha,
_ta4 = fourthTA; _akash , _alind ,
_ta1.runRefresherModule(); _abhiprayah);
} }
/* additional methods elided */ }
}
33
Andries van Dam © 2016 9/22/16
Associations Example (4/4)
public class Course {

● What if we want the TAs to private


private
Professor _vivek;
TA _akanksha;
know about Professor private TA _akash;
private TA _alind;
too? private TA _abhiprayah;

● Need to set up another public Course() {


_akanksha = new TA();
association _akash = new TA();
_alind = new TA();
_abhiprayah = new TA();
● Can we just do the same _vivek = new Professor(_akanksha,
thing? _abhiprayah);
_akash , _alind ,

}
}

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… }

● What can we try instead?


Andries van Dam © 2016 9/22/16
35
Associations Example (4/4)
public class Course {

private Professor _vivek;


● Need a way to pass _vivek private TA _akanksha;
private TA _akash;
to _akanksha, _akash, private TA _alind;
private TA _abhiprayah;
_alind and
public Course() {
_abhiprayah after we _akanksha = new TA();
instantiate _vivek _akash = new TA();
_alind = new TA();
_abhiprayah = new TA();
● Use a new method, _vivek = new Professor(_akanksha,
_akash , _alind ,
setProf, and pass each TA _abhiprayah);
_vivek
}
}

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

● What happens if setProf is never called?


● Will the TAs be able to call methods on the
Professor?

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

You might also like