Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Relationships between Objects-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

This lecture was delivered by Agstya Rao at Bidhan Chandra Krishi Viswa Vidyalaya for Object Oriented Programming course. It inlcudes: Relationship, Objects, Inheritance, Association, Polymorphism, Dynamic, Binding, Dependency, Aggregation, Composition

Typology: Slides

2011/2012

Uploaded on 07/17/2012

banani
banani 🇮🇳

4.3

(3)

91 documents

1 / 9

Toggle sidebar

Related documents


Partial preview of the text

Download Relationships between Objects-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

1

Relationships

between Objects

Lecture 6

2

Overview

n What are different kinds of relationships between classes/ objects? n Inheritance n Association n Dependency n Aggregation n Composition n Polymorphism n Dynamic Binding

3

BASIC TERMINOLOGY

n Objects

n Messages and methods

n Classes

n Inheritance

n Polymorphism

n Dynamic Binding

4

Defining a Class

n When programming a class the data and responsibilities will become: n class variables i.e. values stored in the class as a whole n instance variables i.e. values stored in each instance n class methods i.e. methods the CLASS can do n instance methods i.e. methods an INSTANCE of the CLASS can do (after

5

Example of a Class

n A Customer Class may have: n class variables - totalNumberOfCustomers n instance variables - customerAddress n class methods - new (how to create a new Customer) n instance methods - updateAddress

6

UML Representation of a Class

7

Different sorts of classes

n Abstract Class a class that can have one or more subclasses, but NOT an

n i.e. an abstract class can only be inherited from by another class, it cannot be

n Concrete Class a class that can have one or more subclasses and/or instances

8

Modular Software is like Lego

n Each class is a brick and they can be fitted together in many different ways to form a computer system

9

Execution of programs

n An OO program runs by the coordination of objects receiving, interpreting and responding to messages.

n During the program execution: n objects are created as needed n messages move between objects & methods are activated n objects deleted when no longer required

10

Relationships between Classes

n There are two types of

relationships between different

objects/classes :

n inheritance is_type_of - inherits properties from another class n association uses_a - sends a message to another object/class

11

Types of Association

n dependency depends_upon rely upon other objects

n aggregation consists_of - built up of other objects/classes

n composition consists_of - strongly owns other objects/ classes

12

Inheritance

13

Inheritance

n Inheritance is a key concept of OO paradigm n Like a set of similar objects is described by a class, Inheritance can be used to relationships of similarity

n Example: MonthlyPaidEmployee and HourlyPaidEmployee

14

How it works?

n The approach enables generalisation to occur – means of managing complexity n Code reuse without duplication n A language-provided mechanism for sharing the data/ methods amongst the classes and objects n Allows extension of the hierarchy n By enables creation of new classes from existing classes by only considering the differences

15

Example of Inheritance

e m p lo y e e N u m b e r : i n t^ E m p lo y e e d a t e O f B i r t h : D a t e n a m e : S t r i n g o r g a n i z a t i o n : S t r i n g d e p a r t m e n t C o d e : in t p o s i t i o n C o d e : i n t d a t e O f A p p o in t m e n t : D a t e

M o n t h lyP a id E m p loy e e m o n t h l y S a la ry : flo a t

W e e k lyP a id E m p lo y e e w e e k lyW a g e : flo a t

H o u rlyP a id E m p lo y e e h o u r l y R a t e : f l o a t h o u r s W o r k e d : f l o a t

E m p lo y e e e m p lo y e e N u m b e r : in t d a t e O f B i r t h : D a t e n a m e : String o r g a n i z a t io n : String d e p a r t m e n t C o d e : in t p o s i t i o n C o d e : i n t d a t e O f A p p o i n t m e n t : D a t e

16

Rules of Inheritance

n The subclass always inherits all the characteristics of its ( )

n The definition of the subclass always includes at least one detail not derived ( )

17

Terminology

n HeadOfDepartment inherits from Lecturer

n HeadOfDepartment is a subclass of Lecturer

n HeadOfDepartment is a specialization of Lecturer

n HeadOfDepartment is more specialized than Lecturer

n HeadOfDepartment is a refinement of Lecturer

n Lecturer is a super class (or base class) of HeadOfDepartment

n Lecturer is a generalization of HeadOfDepartment

18

Generalization vs. Inheritance

n Generalization/ specialization represents conceptual relationship between classes n Subclass/ superclass represents concrete relationship between classes n If one class is a subclass of the other, may specialization of the latter. n Conceptual gulf is NOT RECOMMENDED!

19

Identifying Inheritance and its

n The key phrase for finding inheritance is X is a kind of Y n Inheritance can be: n single - each class has only one superclass to inherit from n multiple - each class can take properties

20

Multiple Inheritance (1)

n Car inherits some properties from LandVehicle : n ( Data ) four wheels, red n ( Behaviour ) starts, stops n Car inherits some properties from HouseHoldItems: n ( Data ) owner, address n ( Behaviour ) getsNewOwner LandVehicle HouseHold Items

Car

21

Multiple Inheritance (2)

n Multiple inheritance gives advantages especially in terms of re-usability, but has practical problems n What if both LandVehicle and HouseholdItems have different methods both called moveLocation - which would Car inherit?

22

UML Representation of

Inheritance

e m p lo y e e N u m b e r : in t^ E m p lo y e e d a t e O f B ir th n a m e : S t r in g : D a t e o r g a n iz a t io n d e p a r t m e n t C o d e : S t r i n g : in t p o s i t i o n C o d e : int d a t e O f A p p o in t m e n t : D a t e

M o n t h lyP a id E m p lo y e e m o n t h l y S a la r y : f lo a t W e e k lyP a id E m p lo y e e w e e k lyW a g e : f lo a t H o u r l y P a i d E m p l o y e e ho u r ly R a t e : f l o a t ho u r s W o rk e d : f l o a t

23

A Question

n Consider n HoD and Lecturer n One is the specialization of the other n Both can perform a particular service, e.g.

n It is possible that they perform the same task differently n How to implement it?

24

The Answer:

Method Overriding

n Encapsulation hides the

implementation

n If the object HoD receives a message to perform an operation which both the and the Lecturer can do, then the method invoked will be the specialized version provided by the class HoD!

25

Summary

n Types of classes, attributes and

n Introduction to relationships between

n Inheritance

n Inheritance…