Something abt PoLymOrphISM

A most prominent interview question regd OOPs


I: What is Polymorphism?
S: (Ha ha ;D) Polymorphism is of two types runtime and compile time.
Methods of Compile time polymorphism are Function Overloading and Operator Overloading
Methods of Runtime polymorphism are virtual functions
I: Kool....What is virtual functions or Method Over riding?
S: (Super :D :D)It is possible that we can assign reference of a derived class object to base class
In Such a case if derived class implements a function with same signature as its base then an ambiguity will be arised on resolving function call
So, inorder to overcome that we use virtual functions.
I: Good... Why are they refered as runtime polymorphism? What do u mean by runtime??
S: (Pochu da :( :( ) "Actually" the resolution of function call takes place at runtime. "That is" Compiler don't know which function to call ... Blah Blah ....
I: Why should the resolution take place at runtime? Why not at compile time??
S: (Nammala inniki pottaanga :( :( :( ) new operator allocates memory dynamically so ........
I: K U can leave for the day :D :D


So, Finally a interview seemed to go good :) failed :(


I'm jus going to blaber something which might help u in this particular situation


Virtual functions are referred to Runtime polymorphism bcoz
public class animal // Abstract Class
{
    public void eat()
}
public class cat extends animal
{
    public void eat() { } // Implements eat
}
public class dog extends animal
{
    public void eat() // Implements eat
}
----------------
Some where in the code

animal a=null;
if(i==true)
{
    a=new cat();
}
else
{
    a=new dog();
}
a->eat();
So in the above example the value of a is determined by i which can be determined only at runtime.
IFF the value of a is determined a->eat can be resolved hence they are called as runtime polymorphism


Anyother explanations are welcome :)

Comments

Popular posts from this blog

Headless Chrome/Firefox Selenium Java in Amazon EC2

Cassandra PHPCassa & Composite Types

Selenium Webdriver in Nodejs + Javascript