GeekInterview.com
Series: Subject: Topic:
Question: 505 of 994

What is Runtime Polymorphism?

Asked by: Interview Candidate | Asked on: Dec 21st, 2005
Showing Answers 1 - 5 of 5 Answers
Ravindra

Answered On : Dec 22nd, 2005

In runtime polymorphism ... the code is called at run time according to need or given conditions.

suppose there r two methods namely Add() one in super class and other is in sub class.both have the same name and same parameters.

so we have to choose that which method from them shld called at run time i.e. of super class or of sub class.by polymorphism we do that.

ex:-

 class A

{

int add(){//code of the method}

//some other code

}

class B extends A

{

int add(){//code of the method}

//some other code

}

class AB

{

public static void main(String s[])

{

A ob1;

ob1=new A();

int i=ob1.add();//will call the method of super class.

ob1=new B();// sub class's reference can be assigned to super class address but not vice versa.to do that we have to type cast the reference of the sub class in reference of the super class.

int j=ob1.add();//will call the method of sub class

}

}

  
Login to rate this answer.
sangeetha

Answered On : Jul 25th, 2007

Runtime polymorphism means super class and it's sub class having the same function. whenever the function called by the main method depending upon the parameter the function in super class or sub class will called. I think like that...........

  
Login to rate this answer.
jaipal213

Answered On : Sep 3rd, 2007

View all answers by jaipal213

Runtime polymorphism is also called as inclusion polymorphism. This is achieved through the use of virtual functions.
This is can be better explaned by the following example.

Assume that the Class A implements a virtual method M and clases B and C that are derived from A override the virtual method M. When B is cast to A, a call to the method M from A is dispatched to B. Similarly, when C is cast to A, a call to M is dispatched to C. The decision on exactly which method to call is delayed untill runtime.

  
Login to rate this answer.
phaniraju.vit

Answered On : Sep 24th, 2008

View all answers by phaniraju.vit

It is also called dynamic method dispatch.when super class reference is reffering to subclass object and method overdingis implemented.
Example:


      class figure
{
 private int dim1,dim2,d;
 public figure(int d1,int d2)
 {
  dim1=d1;
  dim2=d2;
 }
 public int getdim1()
 {
  return dim1;
 }
 public int getdim2()
 {
  return dim2;
 }
 public  int  area()
 {
     return d;
 }
};
class rectangle extends figure
 {
  public rectangle(int  d1,int d2)
  {
   super(d1,d2);
  }
  public int area()
  {
  return(getdim1()*getdim2());
    }
 }

 class  DMD
 {
  public static void main(String [] args)
  {
          figure f;
    int d;
         rectangle r=new rectangle (10,5);
    f=r;
    d=f.area();
    System.out.println("the area ist" +d);
  }
 }
 
 
 

  
Login to rate this answer.
parul agarwal

Answered On : Jul 31st, 2011

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementation. This is one of the basic principles of object oriented programming.

The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.