Note: Before using this code please create a Java Project in Eclipse IDE and save this code in TestRTP.java file
Please see the output and understand the concept by reading the explanation. Given below this code
import java.util.*;
class Father
{
public String name = "Satnam";
public static void eating()
{
System.out.println(" is eating fruits");
}
public void drinking()
{
System.out.println(" is drinking coconut water");
}
}
class Son extends Father
{
public String name = "Gurnam";
public static void eating()
{
System.out.println(" is eating pasta");
}
public void drinking()
{
System.out.print(" is drinking mango juice\n");
}
}
public class TestRTP
{
public static void main(String[] args)
{
Father f = new Son(); // assume it is line no-1
System.out.print(f.name); // assume it is line no-2
f.eating(); // assume it is line no-3
System.out.print(f.name);
f.drinking(); // assume it is line no-4
}
}
Output of code =>
Satnam is eating fruits
Satnam is drinking mango juice
Explanation of code =>
Father is a super-class and Son is a subclass. In subclass we are re-defining the static-method and non-static-method of super-class. When a subclass redefine the same non-static-method of super-class, this process is known as method-overriding.
At line-1 we are taking a reference-variable of super-class(ie. father) and assigning the object of subclass (ie. son) into it (this process is known as polymorphic assignment).
At line-2 when we access the variable 'name', the 'name' of father will be printed since the data members (i mean variables) are mainly bound to the type of object rather than the object itself.
The same process will be followed at line-3 when we call a static-method (static-method of super-class will be invoked).
But when we come to line-4 the whole scenario will be changed.
At line-4 the non-static-method of sub-class will be invoked.
Reason, the overridden non-static-method are bound to object rather than the type of object.
So even the reference-variable belongs to super-class the object is of subclass, so in this case the method of subclass (ie. son) will be invoked.
Please see the output and understand the concept by reading the explanation. Given below this code
import java.util.*;
class Father
{
public String name = "Satnam";
public static void eating()
{
System.out.println(" is eating fruits");
}
public void drinking()
{
System.out.println(" is drinking coconut water");
}
}
class Son extends Father
{
public String name = "Gurnam";
public static void eating()
{
System.out.println(" is eating pasta");
}
public void drinking()
{
System.out.print(" is drinking mango juice\n");
}
}
public class TestRTP
{
public static void main(String[] args)
{
Father f = new Son(); // assume it is line no-1
System.out.print(f.name); // assume it is line no-2
f.eating(); // assume it is line no-3
System.out.print(f.name);
f.drinking(); // assume it is line no-4
}
}
Output of code =>
Satnam is eating fruits
Satnam is drinking mango juice
Explanation of code =>
Father is a super-class and Son is a subclass. In subclass we are re-defining the static-method and non-static-method of super-class. When a subclass redefine the same non-static-method of super-class, this process is known as method-overriding.
At line-1 we are taking a reference-variable of super-class(ie. father) and assigning the object of subclass (ie. son) into it (this process is known as polymorphic assignment).
At line-2 when we access the variable 'name', the 'name' of father will be printed since the data members (i mean variables) are mainly bound to the type of object rather than the object itself.
The same process will be followed at line-3 when we call a static-method (static-method of super-class will be invoked).
But when we come to line-4 the whole scenario will be changed.
At line-4 the non-static-method of sub-class will be invoked.
Reason, the overridden non-static-method are bound to object rather than the type of object.
So even the reference-variable belongs to super-class the object is of subclass, so in this case the method of subclass (ie. son) will be invoked.
This is a very importent question which is asked evrytime in interview.
ReplyDeleteSo now this is very helpful for everyone. I have given many interviews and every where this question is asked.
Nice Question..............
ReplyDeleteThank you for sharing Sir.
ReplyDeleteReally very nice informative... I am searching answer for this question in many sites but am not clarified before referring your post.
ReplyDeleteJava Training in Velachery
great explanation sir!!!!
ReplyDelete