Sum*_*boj 5 java oop overriding interface tostring
根据oops的概念.在向上转换的情况下,父类的引用变量不能执行子类的个人方法,但是当我运行以下程序时 -
interface My
{
}
class my1 implements My
{
public String toString()
{
return "hello";
}
public static void main(String... s)
{
My m=new my1();
System.out.println(m.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
它成功执行并打印"你好".任何人都可以解释这是怎么回事...... ??
为了更清楚,以下程序给出了编译时错误
interface My
{
}
class my1 implements My
{
public String toString1()
{
return "hello";
}
public static void main(String... s)
{
My m=new my1();
System.out.println(m.toString1());
}
}
Run Code Online (Sandbox Code Playgroud)