【发布时间】:2020-03-07 21:41:57
【问题描述】:
我在子类中覆盖了父类的 2 个方法,但我没有得到
Object ooj = new String("abc");是如何工作的我认为运行时对象是字符串类型,但是当我将
obj传递给函数时,它会调用Object类型的方法
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);
}
}
我的问题是为什么它调用Object 方法而不是字符串
【问题讨论】:
标签: java polymorphism