I am overriding 2 methods of parent in child class and I'm not getting how
Object ooj = new String("abc");
is workingas per I'm considered runtime object is of string type but when I pass
obj
to function it invokes the method withObject
type
PARENT CLASS
public class Parent {
public void function(Object obj) {
System.out.println("I'm parent Object Type");
}
public void function(String str) {
System.out.println("I'm parent String");
}
}
CHILD CLASS
public class Chile extends Parent{
public void function(Object oob) {
System.out.println("I'm child object");
}
public void function(String str) {
System.out.println("I'm child string");
}
public static void main(String[] args) {
Parent p = new Chile();
Object obj = new String("Gaurav");
p.function(obj);
String str = new String("and");
p.function(str);
}
}
My question is why it is invoking Object
method rather and why not string on