Search This Blog

Monday, July 9, 2012

Relations, Polymorphism, Inheritance...


Today i will be discussing about relationship among objects , polymorphism and inheritance

Association:
    Objects are meant to interact with each other in some way or other. Classes which are completely independent doesn’t need to exist at all. Association is one form of relationship between objects.
The following points will help you understand association better,
  • In association two objects can have separate lifetime i.e creation/destroyal of one object is not going to affect the other object in relation in anyway.
    Example: 1. A very simple example i can think of is doctor patient relationship. A doctor may be handling many number of patients. If a patient dies it’s not going to affect the doctor.
And a patient can be having treatment from many doctors. Even if a doctor is unavailable for any reason he is still going to be a patient. Nothing changes that.
    2.Another similar example is relationship between student and teacher.
    3.Yet another example is a bus can have many drivers and a driver can be associated with many buses. Happens in every city based on shifts.

class doctor{
    patient_list;
    give_treatment();
    study();
    discharge();
} ;

class patient{
    doctor_list;
    get_consulting();
    eat_medicine();
    settle_bills();
};

In the above pseudo code the classes doctor and patient have Association between them.
In the doctor patient association multiplicity of the relation is many to many.
A doctor can have many patients and a patient may in turn be connected with different doctors.

Aggregation:
It's a special form of association where you have a parent child relationship. A child cannot have different parents.
Example: 1.let’s take the most obvious example. A father has 2 children. The children however have only one father !!
2. Another simple example is pen and pen stand. A pen can exist independently without a pen stand and in a similar way a pen stand can exist independently without pen. The point is that a pen cannot be in more than one pen stand.
Composition:
Composition is a special type of aggregation where the child object loses its life cycle if the parent object gets deleted.
Example: One very simple example i can think of is questions and options. An option can belong to only one question and a question can have many answers. In addition to this if a question is deleted then all the options associated with the question also get deleted !!
This has one to many relationship.

An example of one to one relationship would be , relation between a person and his passport or pan card !

We now move on to polymorphism..

Polymorphism:
We have been taught from our schools that function overloading and operator overloading are examples of polymorphism. This concept has been etched so hard in our mind that we have forgot what is polymorphism actually. Polymorphism is defined as occurrence of something in different forms.
Polymorphism is just a concept. To understand it first lets see different types of polymorphisms..
Ad-hoc:
The well known function overloading and operator overloading comes under this category !
Example:
class A
{
    disp()
    {
        print “empty”;
    }
    disp(int a)
    {
        print “an integer”;
    }
}
here the method disp of class A is overloaded.

Subtype polymorphism:
A code working for a general type works also for a specific type would be an example of subtype polymorphism. Example: A function working with numbers in general will work with floating point and a function working with floating point will work well with integers.

float calculate_prod(float a , float b)
{
    return a*b;
}

This function will work well if integers are passed as arguments. (In most languages)

Parametric polymorphism:
Sometimes generic codes are written to accept data of any type. Such type of code delivers parametric polymorphism. (named so because any type of parameter can be given as input).
In C++ templates allow users to create parametric polymorphism.
Example:

template <typename Type>
Type add(Type a, Type b) {
   return a+b;
}


The above add function has been made generic with the use of template.

The above were the basic types of polymorphism.

There is also this thing called polymorphic class.
Consider the following example,
there is a Student class and the following classes are inherited from it,
Geek , Last_bencher .. lets assume there are enough data and methods specific enough to justify this form of inheritance..
Now the Student class can be considered polymorphic. A student can behave like a geek or like a last_bencher depending on the object instantiation. I assume you can understand the core meaning what i intend to deliver.
Function overriding:
Function overriding happens almost every time when inheritance is involved.
Example:
class A
{
    disp()
    {
        print “in A”
    }
}
class B:public A
{
    disp()
    {
        print “in B”
    }
}
In the above pseudo code, the function disp is overridden in the child class B.
So on calling disp using object of class B “in B” will be printed. That is base class method gets overridden.
It doesn’t mean always child method will only be called.
Example:
When you type cast object of B with A class..like
A *a=new B;
a->disp();
Now only base class’s disp is called..
To avoid this virtual keyword is used. If the virtual keyword is used i base class function..
virtual disp()
{
    print “in A”
}
then this base class version of disp will never be called using an object of child class. Always the overridden method will be called.
This binding is decided only during run time. So inheritance along with virtual functions help us in understanding what Run time polymorphism is !

Inheritance:
It's another common OOPS concept.. Inheritance means deriving a subclass from a parent class. Why it's done? So that you can access all the public members of parent class in its subclass. It greatly increases code re-usability. Otherwise you would be copy pasting code everywhere. You may feel what’s big deal in copy pasting. But then using inheritance has the advantage that a change in base class will easily be reflected everywhere !! Sounds cool right?
  • Single
  • Hierarchical  
  • Multiple
  • Multilevel
  • Hybrid are the types of inheritance..
so that’s it for today.. see you again with more concepts in days to come !


Trained @ Sourcebits

2 comments:

  1. The way you explain the virtual keyword, is almost professional. Not many teachers teach C# with the knowledge of virtual, sadly. Good initiative self blogging. :)

    ReplyDelete