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 the meaning of flag day?

622


What is the synchronized method modifier?

692


What are the different ways to handle exceptions?

621


Can an integer be a string?

607


What is string pooling concept?

649






What is a method signature java?

690


Can we use return in constructor?

573


Can we have more than one package statement in source file ?

668


What do you understand by looping in java? Explain the different types of loops.

655


What is boolean data type in java?

655


Can an unreferenced object be referenced again?

834


What are scalar data types?

603


What is a singleton factory?

605


What is import java util arraylist?

745


How to print an arraylist in java?

594