info@meritcampus.com    +91-85006-22255
...
`

Generics Wildcards2768
What will be the output of the following program?
`public class BoundedWildCardDemo {    public static void showXYZ(Coordinates<?> c)    {        System.out.println("X Y Z Coordinates :");                        for(int i = 0; i < c.coords.length; i++)        {            System.out.println(c.coords[i].x+"    "+c.coords[i].y+"    "+c.coords[i].z);        }    }    public static void main(String arg[])    {                   ThreeD threeD[] ={            new ThreeD(1,1, 1),new ThreeD(7, 9, 6), new ThreeD(18, 4, 1)        };        Coordinates<ThreeD> threeDCoord = new Coordinates<ThreeD>(threeD);        showXYZ(threeDCoord);    }}class TwoD{    int x, y;            public TwoD(int x, int y) {        this.x = x;        this.y = y;    }}class ThreeD extends TwoD{    int z;    public ThreeD(int x, int y, int z) {        super(x, y);        this.z = z;    }}class Coordinates<T extends TwoD>{    T[] coords;    Coordinates(T[] coords)    {        this.coords = coords;    }}`

 A. X Y Z Coordinates :1    1    11    1    11    1    1 B. X Y Z Coordinates : C. X Y Z Coordinates :1    1    17    9    618    4    1 D. Compilation Error E. Runtime Error
Topic:

User comments below.All of them might not be correct.

ans c.in main method array is stored with 3 objects.1st object pass 1,1,1 argument. threed class inherits all the features of twoD class .thn values are assigned to one generic array.similarly for othr 2 obj.showxyz() method called for loop prints xyz coordinates

Posted by Maheshwari Natarajan    2014-11-06 08:53:25

Answer c. threeD array is initialized in main method with threeD object.1st obj created and 1,1,1 arguments passed.threeD class extends TwoD so it can use all the features of twoD.contructors are used to to initialize the values.similarly 2nd threeD objct created arguments 7,9,6 passed,3rd pass 18,4,1 arguments.thn coordinate obj created .showxyz method called thr for iterates n sop prints values from coords array object

Posted by NagaRaj Nataraj    2014-11-06 12:13:56

Ans is D...here we have threeD class which is child class of the twoD class,in the main class we create array of threeD class objects....next we create an object for the class Coordinates by passing the created array...here Coordinated class Accepting the Child classes of the towD..by using the bounded types...from main class we are calling thee showXYZ() there we are printing the values of objects which are present in the threeD array which we create in the main method...than here z is not present the Parent Class(TwoD) so that compiler wil raise an error...

Posted by Uday Kumar    2014-11-06 13:48:47

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-07 04:22:54