what is run time polymorphism

Answer Posted / durgasri

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

}

}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference overloading and overriding?

584


How do you reverse a string in java?

590


What do you mean by scope of variable?

491


What is double in java?

522


Can we call the run() method instead of start()?

600






What is an argument in java?

515


How are destructors defined in java?

580


What is the difference between logical data independence and physical data independence?

544


When can you say a graph to be a tree?

645


Do we have pointers in java?

540


What is function and method in java?

529


What are the steps that are followed when two computers connect through tcp?

526


What are the differences between string, stringbuffer and stringbuilder?

531


What is a class variable?

576


What are runtime exceptions?

620