Menu
Topics Index
...
`

Creating Generic Class Method and Variables


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Generic Class Method and Variables
What will be the output of the following program?
public class Generic<T> {
    private T t;
    public void set(T t) { this.t = t;  }
    public T get() {
        return t;
    }
    public <U extends Number> void inspect(U u) {
        System.out.println("T: " + t.getClass().getName());
        System.out.println("U: " + u.getClass().getName());
    }
    public static void main(String[] args) {
        Generic<Integer> integerBox = new Generic<Integer>();
        integerBox.set(10);
        integerBox.inspect(new Number("12"));
    }
}

A. T: java.lang.Integer
U: java.lang.Integer
B. T: java.lang.Integer
U: java.lang.Number
C. Compilation Error
D. throws NumberFormatException
E. Compilation Error or Runtime Error
Topic: Java Generics

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 C compilation error.. as Number class doesn't have a constructor to accept string argument

Posted by Krishna Sai    2014-07-08 12:42:40


ans c .
integrbx is type of generic<integer>,
it can't be instantiate Number type.
compilatin err

Posted by Maheshwari Natarajan    2014-07-08 12:56:23


Ans is C: class is abstract cant be instaniated

Posted by Uday Kumar    2014-07-08 13:17:56


Answer c.
Integer type ly support int type.
it does't suprt Number clas.
but if we give generic<Number> thn it suprt int,long,short values.
to avoid this err remov                     integer box .inspct(new Number("12");
and insert this line
integerbx.inspct(new Integer("12"));

we can use with or without quote

it give result option a.

Posted by NagaRaj Nataraj    2014-07-08 13:19:49


Congratulations Krishna Sai Mulpuri. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-09 04:45:19


© meritcampus 2019

All Rights Reserved.

Open In App