In which of the following case i get output "Hello World" ?
如果子類不覆蓋超類所覆蓋的字符串,則輸出是什麼?
class A{
@Override
public String toString() {
return "Hello World";
}
}
public class B extends A{
public void print(){
//replace code that displays "Hello World"
}
public static void main(String args[]){
new B().print();
}
}
I.的System.out.println(新A());
二, System.out.println(new B());
III.System.out.println(this);
1. only I
2. I and III
3. I and II
4. all I,II and III
其answer is 4
即all I,II and III
i understood about I but why II and III is also right ?
EDIT : Also specify which section of jls provides this specification ?
'toString()'被所有子類覆蓋。 – Santosh
但是爲什麼在創建子類實例的情況下打印輸出到超類的字符串 – Prateek
請檢查我的答案。 – Santosh