Class 13
Class 13
Concepts
1
Definitions
▪ Class
▪ Objects
▪ Inheritance
▪ Abstraction
▪ Encapsulation
▪ Polymorphism
2
What are the Different Types of
Relationships Among Classes?
⚫ Four types of relationships:
− Inheritance
− Association
− Aggregation/Composition
− Dependency
3
Multiple Inheritance
Derived
Faculty Students Staff Faculty Students Staff
Classes
Multiple
Inheritance
4
Association Relationship
⚫ Enables objects to communicate with
each other:
− Thusone object must “know” the
address of the corresponding object in
the association.
⚫ Usually binary:
− But in general can be n-ary.
5
Association Relationship
1 borrowed by *
Library Member Book
6
Association Relationship
⚫ A class can be associated with itself
(recursive association).
− Give an example?
⚫ An arrowhead used along with name,
indicates direction of association.
⚫ Multiplicity indicates # of instances
taking part in the association.
7
Aggregation Relationship
⚫ Represents whole-part relationship
⚫ Represented by a diamond symbol at
the composite end
⚫ Cannot be reflexive (i.e. recursive)
⚫ Not symmetric
⚫ It can be transitive
8
Aggregation Relationship
* Paragraph * Line
1 1
Document
9
Composition Relationship
1 *
Order Item
10
Aggregation vs. Composition
⚫ Composition:
− Composite and components have the
same life.
⚫ Aggregation:
− Lifelines are different.
⚫ Consider an order object:
− Aggregation: If order items can be
changed or deleted after placing the
order.
− Composition: Otherwise. 11
Class Dependency
12
Abstraction
⚫ Consider aspects relevant for
certain purpose
− Suppress non-relevant aspects
⚫ Types of abstraction:
− Data abstraction
− Behaviour abstraction
13
Encapsulation
m4
m3
m5
m2 Data
m6
m1
Methods
Concept of encapsulation 14
Polymorphism
⚫ Denotes poly (many) morphism
(forms).
⚫ Under different situations:
− Same message to the same object
can result in different actions:
⚫ Static binding
⚫ Dynamic binding
15
An Example of Static Binding
− Class Circle{
⚫ private float x, y, radius;
⚫ private int fillType;
⚫
16
An Example of Static Binding
cont…
⚫ A class named Circle has three definitions for
create operation
− Without any parameter, default
− Centre and radius as parameter
− Centre, radius and fillType as parameter
− Depending upon parameters, method will be invoked
− Method create is said to be overloaded
17
Dynamic Binding
⚫ A method call to an object of an
ancestor class:
− Would result in the invocation of the
method of an appropriate object of the
derived class.
⚫ Following principles are involved:
− Inheritance hierarchy
− Method overriding
− Assignment to compatible types
18
Dynamic Binding
⚫ Principle of substitutability (Liskov’s
substitutability principle):
− An object can be assigned to an object of
its ancestor class, but not vice versa.
A A a; B b;
a=b; ??
B
b=a; ??
19
Dynamic Binding
⚫ Principle of substitutability (Liskov’s
substitutability principle):
− An object can be assigned to an object of
its ancestor class, but not vice versa.
A A a; B b;
a=b; (OK)
B
b=a; (not OK)
20
Dynamic Binding
Cont …
21
Genericity
⚫ Ability to parameterize class
definitions.
⚫ Example: class stack of different types
of elements:
− Integer stack
− Character stack
− Floating point stack
⚫ Define generic class stack:
− Later instantiate as required
22
Genericity
T
Set
insert(T)
remove(T)
Refinement
EmployeeSet
23
Thank You
24