OverridingDemo.java
class Parent
{
public void show(int... i)
{
System.out.println("parent");
}
}
class Child extends Parent
{
public void show(int[] i)
{
System.out.println("child");
}
}
public class OverridingDemo
{
public static void main(String[] args)
{
Child c = new Child();
int arr[]=new int[]{2,3};
c.show(arr);//child
c.show(2,3,4);//error
}
}
Answer:- Its method Overriding. int[] i and int …i both internally represents an Array. The only difference is while calling show(int[] i) you have to pass array/null.You cannot pass no of arguments to it But while calling show(int … i),You can pass array or any number of arguments,or null As the method is overrided,any call to c.show(-),will always invoke show(int[] i),and if you pass integer array,it works fine. Else compile time error Caution:- In JDK build 1.7.0_03-b05,Its output is as explained above. In JDK build 1.6.0_32-b05,There is no error which calling c.show(2,3,4),and it gives child as ouput In JDK build 1.5.0_10-b03,show(int …) & show(int a[]) are treated as same,so it wont allow overriding,because both the show() are treated as same,and you cannot have 2 method with same signature
0 comments:
Post a Comment