Answer Posted / anjani kumar jha
Java supports two types of polymorphism
1)compile time
2)Run time
1)Compile:---overloading is the best example of compile-
time polymorphism.
Look at this example:----
class NewClass {
public void aSum()
{
System.out.println("hi.......");
}
// NewClass nc1=new InheritClass();
// NewClass nc=new NewClass();
}
class InheritClass extends NewClass
{
@Override
public void aSum()
{
System.out.println("over-riden......");
}
public static void main(String as[])
{
NewClass nc=new NewClass();
NewClass nc1=new InheritClass();
nc.aSum();.......compile time polymorphism
reference type determine which method to be invoke
//nc1.aSum();
}
2)Run time:Over-ridding is the example of run time
polymorphism........
look at example----
class NewClass {
public void aSum()
{
System.out.println("hi.......");
}
// NewClass nc1=new InheritClass();
// NewClass nc=new NewClass();
}
class InheritClass extends NewClass
{
@Override
public void aSum()
{
System.out.println("over-riden......");
}
public static void main(String as[])
{
NewClass nc=new NewClass();
NewClass nc1=new InheritClass();
// nc.aSum();
nc1.aSum(); //object type determine which method
shuold call
//run the example u will get idea about that
}
}
}
| Is This Answer Correct ? | 5 Yes | 1 No |
Post New Answer View All Answers
How do you define social class?
i am getting an of the type can not convert int to int *. to overcome this problem what we should do?
What are the advantages of polymorphism?
What is the fundamental idea of oop?
What is oops with example?
What is multilevel inheritance?
Can we override main method?
What is use of overloading?
What is encapsulation with real life example?
What is the difference between static polymorphism and dynamic polymorphism?
What is overloading and its types?
What is the difference between abstraction and polymorphism?
IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?
Where You Can Use Interface in your Project
What is overriding vs overloading?