This question is based on the concept that when I store the reference of child class into parents class variable.And, using that variable I call the method that is present in both the classes Child-Parent.Here child method should get evoked and it does but that not happens when I pass the reference as the parameters, why?
class Parent
{
public void show()
{
System.out.println("Parent");
}
}
class Child extends Parent
{
public void show()
{
System.out.println("Child");
}
}
public class Call {
public static void main(String args[])
{
Parent p=new Child();
p.show();
}
}
Output Expected: "Child" Actual Output: "Child" [as expected]
But,
class Parent
{
public void show(Parent x)
{
System.out.println("Parent");
}
}
class Child extends Parent
{
public void show(Child x)
{
System.out.println("Child");
}
}
public class Call {
public static void main(String args[])
{
Parent p=new Child();
p.show(new Child());
}
}
Why the output is "Parent" in this case?