Menu
Question Index
...

What will be the output of the following program?

public class BoundedWildCardDemo {
    
    public static void showXY(Coordinates<?> c)
    {
        System.out.println("X Y Coordinates :");
                
        for(int i = 0; i < c.coords.length; i++)
        {
            System.out.println(c.coords[i].x+"    "+c.coords[i].y);
        }
    }
    public static void main(String arg[])
    {
        TwoD twoD[] = {
            new TwoD(1,1),new TwoD(7, 9), new TwoD(18, 4)
        };
        Coordinates<TwoD> twoDCoord =new Coordinates<TwoD>(twoD);
        showXY(twoDCoord);
    }
}
class TwoD
{
    int x, y;
    public TwoD(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
class Coordinates<T extends TwoD>
{
    T[] coords;
    Coordinates(T[] coords)
    {
        this.coords = coords;
    }
}


X Y Coordinates :
1    1
1    1
1    1
X Y Coordinates :
1    1
7    9
18    4
X Y Coordinates :
Compilation Error
Runtime Error

Doubts

Problems

Topic: Generics Wildcards In Java With Examples

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App