Menu
Topics Index
...
`

Generics Unbounded Wildcard


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics Unbounded Wildcard
What will be the output of the following program?
public class GenericsWildCardDemo1 {
    public static void main(String args[])
    {
        Number[] ob =  {1.2, 2.2, 3.2, 4.2, 5.2};
        StoreNumbers<Number> obj = new StoreNumbers<Number>(ob);
        print(obj);
    }
    static void print(StoreNumbers<?> c)
    {
        for(int i = 0; i < c.obj.length; i++)
        {
            System.out.print(c.obj[i] + " ");
        }
    }
}

class StoreNumbers<T extends Number>
{
    T[] obj;
    StoreNumbers(T[] obj) {
        this.obj = obj;
    }
}

A. 1 2 3 4 5
B. 1.2 2.2 3.2 4.2 5.2
C. Compilation Error
D. Runtime Error
Topic: Generics Wildcards In Java With Examples

If you need explanation Read this topic

If you need Answer Take test on this topic

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

Ans is B...here we have the Wildcard concept..this can be used specify the parameter etc...this will used when we have unknown type

here we StoreNumbers class which can receive the all Child types of the Numbers class...in the main() we create the instance for the StoreNumbers class with the Number class..next we are calling the print() with this object..and there we are receiving with wild card and we are printing the the elements of the array

Posted by Uday Kumar    2014-12-26 13:10:44


ans b.... here the concept of generic wildcards are used wich can be stated as -->>>Java Generic's wildcards is a mechanism in Java Generics aimed at making it possible to cast a collection of a certain class, e.g A, to a collection of a subclass or superclass of A. Question mark (?) is the wildcard in generics and represents an unknown type. The wildcard can be used as the type of a parameter, field, or local variable and sometimes as a return type. We can’t use wildcards while invoking a generic method or instantiating a generic class. number obj is freating an obj of array typ amd storing valyes in it..... the instance of storenumber class is created den after... ...here the Storenumber class is used to recieve all the child type class of the number class.........here in the print(obj) we are fetching the wild card values and then printng the elemwnts of the array

Posted by Asma Mujtaba Khan    2014-12-26 18:00:32


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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-12-29 05:15:10


© meritcampus 2019

All Rights Reserved.

Open In App