Why Over riding is Run Time Polymorphism?

Answers were Sorted based on User's Feedback



Why Over riding is Run Time Polymorphism?..

Answer / anjani kumar jha

It is very diffcult for compiler to know which version of
the method called(superclass or subclass).

Hence to identify which version of method is called java
used run-time polymorphism(same copy used in both class)
where object type defined which version is called.
I am giving u one example............

class A
{
public void sum() //this is method which we will override
{
//some operation here//
}
}
class B extends A
{
public void sum() //over-riden method,see both method
r //same
{
//some operation here//
}
public static void main(String as[])
{
A a=new B() //RUN TIME POLYMORFISM
a.sum()//Since a is a object type of class B SO class B sum
method will be called................................
}
}
//I think uy doubt will be clear
}

Thanks and Regards
Anjani Kumar Jha
CDAC
9623154095

Is This Answer Correct ?    15 Yes 1 No

Why Over riding is Run Time Polymorphism?..

Answer / mani

Overriding method resolution always take care by JVM based
on runtime object. Hence overriding is consider as runtime
or dynamic orlatebinding.

Is This Answer Correct ?    16 Yes 3 No

Why Over riding is Run Time Polymorphism?..

Answer / umanath

This is (Run Time Polymorphism) called Dynamic binding.

for more depth see the bellow example

class A
{
public String msg = "message-A";
public void display()
{
System.out.println(msg);
}
}
class B extends A
{
public String msg = "message-B";
public void display()
{
System.out.println(msg);
}
}

class demo
{
public static void main(String as[])
{
A a = new B() //RUN TIME POLYMORFISM
a.display() //Since a is a object type of class B SO class
B sum
System.out.println("From Main: "+a.msg);
}
}

Out Put:
message-B
From Main: message-A

Is This Answer Correct ?    4 Yes 2 No

Post New Answer

More Core Java Interview Questions

when should you use stringbuilder class in a program?

0 Answers  


what is nested class in java?

0 Answers   IBS,


What is parseint?

0 Answers  


While opening the file, what type of exceptions can be caught?

3 Answers  


How to define a constant variable in Java?

1 Answers   TCS,


Is it possible to use string in the switch case?

0 Answers  


Can a Byte object be cast to a double value?

3 Answers   KO,


What is methods and methodology?

0 Answers  


how to identify duplicate values in arraylist

2 Answers   TCS,


Why string is a class?

0 Answers  


Difference between JVM and JRE?

3 Answers   Amdocs,


JVM responsibility?

6 Answers   TCS,


Categories