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
What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. Import java.net.* Versus import java.net.socket)?
What is early binding and late binding in java?
What is the main use of generics in java?
Can a private method be declared as static?
What is navigable map in java?
What do you understand by weak reference?
What is Hierarchy of exception?
What is printwriter in java?
Why java is platform independent? Explain.
FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?
How does hashset works in java?
Why java strings are immutable in nature?
What is hashmap in java?
Is there is any difference between a scrollbar and a scrollpane?
Explain about the main() method in java?