Saturday, November 10, 2007

Objects relationship in oops

In Object Oriented concepts there are two type relations which exists in between two objects.

1. IS A Relationship
2. HAS A Relationship

Inheritance and “is a”

We can understand this better by considering the “is a” idea

A subclass object “is a” superclass object.However, some extra instance variables and methods may have been added and some other methods may have been changed
Note that “is a” is a one way operation.Subclass “is a” superclass (specific "is a" general)With modifications / additions Superclass is NOT a subclass (general not "is a" specific Missing some properties
Ex: Bird “is a” Animal
Human “is a” Animal
Fish “is a” Animal


Bird, Human and Fish are all Animals
However, an Animal is not necessarily a Bird, Human or Fish

HAS A relationship

In OOP, Has-A relationships are relationships where one object contains, or controls another object. It is contrasted with an Is-A relationship.
Objects having Has-A relationships are models for composition, or an object being contained inside of the parent object.

Examples of Has a Relationships:
A Person object has-a(n) Address Object.

A Teacher Object has-a Student Object.
A Car Object has-a Tire Object.
A Document Object has-a Keyword or Tag Object.
Generically, an Object has-a DAO or Gateway Object
In these examples, you notice that the objects are not related in a parent-child type of relationship. They (typically) cannot respond to the same type of events or messages.
Contrast this with the Is-A relationship that are models for Inheritance.

No comments: