Thursday, 21 February 2013

POLYMORPHISM


One interface multiple methods. i.e one interface is used for different actions
There are two types of polymorphism:

   1)     Compile time polymorphism – function overloading
   2)     runtime polymorphism—dynamic linking

For example : if in a program  we have 3 different types of stacks:
One which calculates int, double and other float..
Then we can have a method named stack (same name) with different types of parameters.(different actions):
  -->stack(int)
  -->Stack(double)
  --->Stack(float)
i.e same name but different type of arguments.

The compilers’s job is to select the right method, programmer doesn't have to select it manually.
But how does Compiler know?? which method to select at compile time?

FUNCTION OVERLOAD RESOLUTION IN JAVA

Java's overload resolution process operates in two phases.

The first phase: For a given function call, find the set of all applicable methods on the basis that there must exist either an exact match or a widening type conversion from each argument in the function call (i.e the actual parameters )to the corresponding parameter of the method being considered. If this set consists of only one method, select that method for invocation. Otherwise proceed to the next step. They are used only to determine which overloadings are applicable

The second phase: It selects the most specific of the methods or constructors selected in the first phase,  it selects the most specific overloading, using only the formal parameters: the parameters appearing in the declaration  . If there exists either an 
1) exact match
 2) widening type conversion
 from each of the parameters of a method in the set to the corresponding parameters of another method, eliminate the latter method from the set. 
If you are left with only one method after pruning the set in this manner, select that method for invocation. Otherwise proceed to the next step.

The third phase: Declare the source code invalid because there does not exist a single maximally specific method that can be invoked for the function call.

Consider the following example:


//Overload.Java

class Employee {String name;}

class Manager extends Employee {int level;}

class Test {
    static void foo(Employee e1, Employee e2) {    //first foo   //(A)
        System.out.println( "first foo");
    }
    static void foo(Employee e, Manager m) {       //second foo  //(B)
        System.out.println( "second foo");
    }
    static void foo(Manager m, Employee e) {       //third foo   //(C)
        System.out.println( "third foo");
    }
    public static void main(String[] args)
    {
        Employee emp = new Employee ();
        Manager man = new Manager();

        foo(emp, man);        // will invoke the second foo      //(D)
        //foo(man, man);      // Error because it produces an    //(E)
                              // ambiguity in overload resolution
  }
}


When we call ---à foo(emp,man)
Phase 1

Find set of applicable methods, i.e exact match and widened type conversion, also here actual parameters  are considered, i.e parameters called during invocation :
A)     foo(Employee e1, Employee e2)-à foo(emp,man)
emp is exact match, man is an employee object( widened type ,comparing with actual parameters)à so pass
B)     foo(Employee e, Manager m) à foo(emp, man)
emp is exact match, man is an exact match with man-à so pass
C)     foo(Manager m, Employee e)à foo(emp,man)
emp object is not always a manager object—not pass (taking formal parameters into consideration)
every manager object is an employee object..
So end of Phase 1: {A, B}

Phase 2: {A, B}
A--> has one exact match and other widened match
B--> have both exact match

So, eliminate the one who has widened match, in our case A
Hence, B--> foo (Employee e, Manager m) would be called

Now consider that happens if we uncomment the call to foo in line (E). Its argument types match the parameter types for all three definitions of foo. So at the end of the first step of overload resolution, we have three candidates in the set.
 By the same reasoning as before, we now discover that the first definition of foo is less specific than the other two. So we delete it from the set. However, we also discover that it is not possible to delete either of the remaining two entries from the set, for the simple reason that an Employee type does not possess a widening type conversion to a Manager type. So we declare ambiguity and, therefore, compile time error results.


If you have many types and this is unmanageable, then method overloading is probably not the right approach, rather the public method should just take Object and implement some kind of strategy pattern to delegate the appropriate handling per object type.

Ability to dispatch a call to a method based on types of arguments is called multiple dispatch. In Java this is done with Visitor pattern..



References:

Saturday, 15 September 2012

God's Gift to me!!!!


The word BFF, says it all!!!! A Best Friend... its someone who you know will ALWAYS be there for you in bad times and  good times.... someone who u can share every little detail of your life... someone who knows u from A to Z, who knows how you'll react in every situation, i may say its someone who know what u want to say or what ur thinkin just by lookin at u... with whom you can be yourself and tell whatever comes in your mind, without giving a second thought of what other will think!!!!
And I am lucky to have that someone in my Life!!, Not everyone is lucky to have BFFs!!! But I am !!!, because I have YOU!!!!!!




She was  a mamma's Girl!!! and was pampered  by aLL l!! and when she became 4, God brought ME in her LyF :)
So, Here goes the story of , How I met my bestie and how we became such close friends!!!


We met as kids in Buds School..(Woww feels soo good by thinking, I have a bestie who knows me since my first school)
I Didnt even know what friends and relations are , but knew one thing, that she is special for me... I was too small to remember routes to places, but somehow  the path which reached her place and her HEART, was imprinted in my mind...( I used to tell my mom "Take a Right Turn from ""BLACK GATE"", after Panchdev Temple" which still exists... and I still get nostalgic , when pass by!!! )


A friend whom have seen since her childhood, have seen her growing up with me!! 

Nits we both grew together like sisters(You are family to me!!!! There were no formalities between us.. We didn't have to invite each other to our places, Every other day we used to plead our moms to let us meet each other.....or for a night stay!!! and woww its still the same ;) (luckily our moms understands now :):))
Have StUdied together, (hmmm  obviously talked more than studying, and specially closing the door and talking !! until someone knocks the door..) 
and  once before exams, I remember aunty taking our exam and giving us a report card with stars( thats way back in Nursery or KG, Have still preserved it  with my report cards:) ).
We have played all kinda games( Playing Teacher Teacher, Ghar Ghar!!! wow amazing days,(Used to collect small fridge , vessels, made rotis tooo;) and playing board Games in Vacations .)





Then slowly we became the best of friends, had sign language to call each other in class also (uhun uhun )(  Unfortunately, the whole class used to know that, its Khushboo and Niyati trying to pass a message  ;);)  )


We had  loads of fights ofcourse...(Some sweet ones which we do every day... How can Niyati not call me Gadhi!!) Few fights because of jealousy.... because couldnt share our friend with anyone else.(Damnnn sooo very possesive we were of each other and still are...Beware of that  Ravish,  and yeahh, you shud listen to our talks sometimes!!!!!)

Then came the crutial period of our lives thats  11th and  12th.. I went for Science, and she went for Commerse... But change of stream couldnt part these best friends away!!!!


The immature Adults(who considered themselves very matured) entered the Golden Period of their lives.. The College Life... again  inspite of being so many Miles away from each other, we made it a point to take out time for each other.. ( with long phone Bills ;) )


The most caring friend , (Cant find a  superlative for this one!!! woww... amazingly cares for her loved ones.... but u need to get into that list of hers... vry few people can get through ;)  )a person can ever get!!!!!! Really lucky to have YOU!!...

Remembers the smallest detail of things u hav ever told her(Good nd  bad for me ;) and then the special dialoogue "Don't try to remember ,  mane badhu yaad chhe" )

Talkative(yeahh sometimes talks a lott, but my ears are always there for her :))

Support System,( whenever in Life , if I look back, I will find her by my side, if am wrong she will scold me , but still will console me at the same time... I know she is one of those who will say "

"I'll be there... whether you say you need me or not." 

She is the one with whom can Share Dark Secrets with...
A typical Shopaholic (Similar to Confessions of a Shopaholic ;))..Woww Shopping wid you have always been soo much fun...

Veryyy Creative and yeah most importantly,  a  person with  a PURE HEART!!!




Woww Nits have so many wonderful moments with youuu....... 

Our ALL Day Shopping Spree!!!! Where we used to eat more than  shopping { Freezeland, Sweets ,Slush, Pani puri, Sev Puri an what not!!!!  } and At the end of the day we used to wonder how come we have spent so much, without shopping ;);)

All our friendship Days and New Year's Eve,  which have been soo special since years.. just because of YOU!!!  and it feels really  incomplete without Youu  Really I know how I felt when was in HYD.. :(:(

If its both of us!! We don't care where we are ,  we can do our Bakwaas Talks!! and time flies sooo fast!!! (Remember  our latest shopping ,  we were talking and laughing, as if noone else is around,  mom had to plead us to check few designs in the shop ;) )

She is the one who is worried of my wedding more than me :) She used to call me and scold me everyday , to come back from HYD soon!!!!



Its truly said, that "You can go through life and make new friends every year - every month practically - but there was never any substitute for those friendships of childhood that survive into adult years. Those are the ones in which we are bound to one another with hoops of steel.”  hmmm fevicol ka Jod Hai, Tutte Ga Nahi!!!! :):)

So today on your birthday, Just a wish for you that, You be happy aLways!!!! Hope all your wishes come true!!! Hope you find your dream Guy who  will come on a White Horse and take  you away !!! Love you loads, And will always be by your side, no matter what.... I know am getting married and will go faraway , but I will just be a call away from You :):)

                                               HAPPY BIRTHDAY ONCE AGAIN!!!!!                                                





Wednesday, 25 July 2012

Daddy's GirL!!!!



Today on your Birthday, I felt like expressing to you about how special you have been in our Lives!!!

What is a father’s love?
A warm hello, a helping hand?
Is it a gentle scolding when you’re bad?
Staying up all night doing homework?

(Always checking on us ""Jaage chhe"" )
Waking up early in the morning before the Exam..
Waiting up for her til she comes home late in the night !!!

(Specially during Navaratri, Sitting on the swing..
And we used to be so sure while entering the society, that we will see you at the door)
A father’s love is all this and more.
And  you have given to us a lott MORE!!!!!!!!!!! 


Papa, you have always been there by our side no matter what, 
You were there, whenever We looked back
You have given us ""how to Live Life Lessons""
along with my math lessons,
You helped us learn to Swim and Swing,To Dive and Drive.

A firm but loving touch is what you gave to us.
You helped me learn to be strong and stand on my two feet. 
Always supported and Encouraged ..You have always been an inspiration.
Today, when I look back, I see that you have never said NO for an answer..
Never gave a second thought about  what  time or distance it is, when coming to drop me to Gnagar/Visnagar, or  A'bad/H'Bad.




And when we got hurt, fell down, or lost our Hopes, you holded our hands with patience and lifted us back, gave us the courage to face the challenges, and  told us to never stop Dreaming!!!
(Telling all this while  applying the ice pack on our wounds , which just vanished over time, with your Care !!)
Or when we used to be afraid telling the marks or result , Only question you would ask, "Fail toh nathi thai ne?? Then don't worry, just to console us.."



Your advice will always be remembered specially the quoted lines, which you have been telling us since long!!:
"Karm Kiye jaa phal ki chinta matt kar"
Or the famous one:
"Try and try until you succeed.."
and yes,  how can I forget the special one, which has always remained close to our hearts:
"Time is less and you have to cover a lot of distance,
Covering the Distance is not your purpose,
You have to revise it twice and thrice"




And then You let this butterfly FLY,  to let her live her dreams..I would have not got this independent life of Hyderabad,  without your support. 
And now at this junction, when am about to get married, I see my childhood and wish it all comes back..


But yes, one things is for sure, No one can take the place  A Girl has for a  father.....


                                 You are my dad, my hero,
                          And you’re the apple of my eye. 
                      Dad I can’t tell you what your love has meant
                        Thank you so much for what you did
                        And for all that you continue to do. 

                                                     HAPPY BIRTHDAY!!!!
                    Wish we could all be with you on this special day of yours!!!


G