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
How java uses the string and stringbuffer classes?
What are different types of constants?
What is array pointers ?
What is the difference between JVM and JRE?
What is final keyword?
What is the basic difference between string and stringbuffer object?
What is a char in java?
Is math class static in java?
What is constructor chaining and how is it achieved in java?
What are the drawbacks of singleton class?
Will minecraft java be discontinued?
What are the different types of inner classes?
What is a dynamic array java?
Given a singly linked list, determine whether it contains a loop or not without using temporary space?
What is the main difference between java platform and other platforms?