Relationships between objects I
Christian Rodríguez Bustos
Object Oriented Programming
Relationships between objects
Agenda
Static Relationships
Inheritance
between Inheritance
Keyword and Java I
objects
Relationships between objects
static Keyword
Static attributes
Static methods
Relationships between objects
Static attributes
Static attributes are
common to all
instanced objects
Static attributes are
class attributes
Relationships between objects
Static attributes example
Relationships between objects
Static attributes example
Student A
Id = 1
Static property Number of students = 1
Relationships between objects
Static attributes example
Student A Student B
Id = 1 Id = 2
Static property Number of students = 2
Relationships between objects
Static attributes example
Student A Student B Student C
Id = 1 Id = 2 Id = 3
Static property Number of students = 3
Relationships between objects
Accessing static attributes
Relationships between objects
Accessing static methods or attributes
We do not need to instantiate
any object to access static
attributes or methods
Relationships between objects
Examples
Static methods typically take all they data from
parameters and compute something from those
parameters.
Relationships between objects
TicTacToe Examples
Relationships between objects
Accessing static methods or attributes
Be careful!!!
Static methods cannot access Non-Static
attributes
Relationships between objects
Relationships between objects
Association and links
Aggregation and composition
UML notation
Associations are structural relationship
Associations are
structural
relationship that
exists between
classes
• A Professor teaches one
or many Groups
• A Course is offered as
one or many Groups
• Zero or many Students
attends one or many
Groups
Relationships between objects
Links
Are relations between two specifics objects
(instances)
Association attends at
: A student Any group
Link: Bruce Wayne attends at Math 3B
A specific student A specific group
Relationships between objects
Higher order associations
One or more Student attends
one or more Groups Student
One or more Students receive
one or more Grades
Group Grade
One Group issues one or
more Grades
Relationships between objects
Higher order associations
Higher order associations are represented by two
classes associations
Relationships between objects
Aggregation and Composition
Aggregation: Is a specific A Team is composed by one or more
Students
type of association, is
represented typically by A Department is composed of one or
more Professors
“consists of”, “is composed
of” and “has a” A Club has Members
Composition: Is a strong A Building is composed by one or more
form of aggregation, in which Rooms
the “parts” cannot exist A University is composed of Departments
without the “whole.”
A Board is composed of Squares
Relationships between objects
Aggregation code example
A Team is composed by one or
more Students
Class definition
If Team is destroyed, the Students Test code
still exist
Relationships between objects
Composition code example
A Board is composed of Squares
Test code
Class definition
If Board is destroyed, the squares are destroyed too
Relationships between objects
UML notation
Composition is
Aggregation is
depicted as a filled
depicted as an
diamond and a solid
unfilled diamond
line.
Relationships between objects
Inheritance
Hierarchy of classes
UML Notation
Actual Situation
Typical Student has an
id and a user
We @override toString
method to print the
Student data
Relationships between objects
New Requirements arrives
The Academic
Information System have
to manage the
information of graduate
students:
– Undergraduate
program Your boss
– Current place of
employ
Relationships between objects
Solution 1
Modify the Student Class
Relationships between objects
Solution 1 - Modify the Student Class
We can add new parameters, setters and
getters and modify the toString method to print
depending of type of student
Relationships between objects
Solution 1 - Modify the Student Class
Relationships between objects
New Requirements arrives
The system must to
handle the graduate
program being taken by
graduate students:
– Current graduate
program
Your boss
Relationships between objects
Solution 1 - Modify the Student Class again ???
You have to add one more attribute, two
methods and a extra validation in the toString
method
Relationships between objects
Solution 1 is a bad solution
Your student class is not
well delimited, at this
moment your objects
students can be
understood as
graduate and
undergraduate.
How can we distinguish
between one or
another?
Relationships between objects
Solution 2
Create GraduateStudent class
Relationships between objects
Solution 2 – Create GraduateStudent class
This makes sense, it could
work!!!
Relationships between objects
Solution 2 – Create GraduateStudent class
Wait!!! , this code smells
like a cloned code!
Relationships between objects
Clones!!!
Relationships between objects
Try to avoid cloning
Clones are hard to
maintain and reflect
poor designs.
Relationships between objects
Solution 3
Taking Advantage of Inheritance
Relationships between objects
Solution 3 - Taking Advantage of Inheritance
Gradate Student inherit all accessible
methods and attributes from Student
class
Relationships between objects
Inheritance terms
Graduate Student Student
is a generalization of a Graduate
is a specialization of Student
Student
is a subclass of Student is the superclass of Student
Relationships between objects
Inheritance terms
A Is a
subclass superclass
A Graduate Student is a Student
A Graduate Student is a specialization of a Student
Relationships between objects
Class Hierarchies
Generalization
Person
We manage knowledge
in terms of inheritance
hierarchies
Student Professor
In a POO language we
can abstract the real
world relation into class
Graduate hierarchies
Student
Specialization
Relationships between objects
Inheritance is one of the four principles of OOP
Abstraction Encapsulation
Inheritance ?????????
Relationships between objects
Inheritance benefits
Reduction of
code redundancy Subclasses are
• Maintenance more concise
• Avoid “Ripple Effects”
We can reuse and
We can derive a
extend code that
new class from an
has already been
existing class
tested
Inheritance is a
natural way to
manage
knowledge
Relationships between objects
Class Hierarchies inevitably expand over time
Person
Student Professor
Graduate
Undergraduate
Student
Relationships between objects
Class Hierarchies inevitably expand over time
Person
Student Professor Administrator
Graduate
Undergraduate
Student
Relationships between objects
Class Hierarchies inevitably expand over time
Person
Student Professor Administrator
Graduate
Undergraduate
Student
Masters PhD
Relationships between objects
UML notation
• A Student is a Person
• A Professor is a
Person
Relationships between objects
Java Inheritance I
The object class
All classes are subclasses of the Object class
In Java Class Object is the
root of the class hierarchy.
Object
Every class has Object as a
superclass. Person String System
All objects, including arrays, Student Professor
inherit the methods of this
class.
Relationships between objects
All classes are subclasses of the Object class
Is equivalent
to
Relationships between objects
All those methods are
inherited by all classes
Relationships between objects
Time to play
1. Using UML design a class hierarchy (at least 3 levels
of inheritance) for a pet store with at least 6 different
kind of pets
2. Create the Java classes definitions (encapsulated)
for the pets available on the pet store, each pet must
have at least 3 attributes (not inherited).
Relationships between objects
References
[Barker] J. Barker, Beginning Java Objects: From Concepts To Code,
Second Edition, Apress, 2005.
[Oracle] Understanding Instance and Class Members, Available:
http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html
[Oracle] Java API documentation, Class Object, Available:
http://download.oracle.com/javase/6/docs/api/java/lang/Object.html
Relationships between objects