Menu
Topics Index
...
`

Generics Consturctor2856


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics Consturctor2856
Is program compiles or not?
public class GenericsType<MC> {
    new MC();
    MC t;
    public void set(MC t1) {
        t = t1;
    }
    public static void main(String args[]) {
        GenericsType<String> type = new GenericsType<String>();
        type.set("Merit Campus");
    }
}

A. Program compiles successful.
B. Compilation Error because we can not create object for MC.
C. Compilation Error because in set() we are not give this.t = t1;.
D. Compilation Error or Runtime Error for some other reason.
Topic: Java Generics In Methods And Constructors

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 D
compilation error of

i)illegal start of type  ...           //new mc();
ii)invalid method declaration ; return type required...........//new mc();

Posted by Vicky Maduri    2015-02-21 15:11:34


ans is D..here we have the generics concept where we can create the different type of the objects for a class..here we have the GenericsType class...next we are creating the instance for the class by passing the String..in the we are creating the new MC() here we dont have any class with the name MC..just it is an Paramater type..we can not create it

Posted by Uday Kumar    2015-02-23 03:36:27


ANS IS D (Runtime Error for some other reason)

--Here Concept of Generics is used.

--Generic Types

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

--Here,genericsType is used in which string "merit campus" is set by using the method set.In method set(),t reference of class Mc is assigned to another reference t of class MC.

--Runtime or Compilation Error comes for some other reason.

Posted by Mânïshå Mùlchåndânï    2015-02-23 17:41:37


This dose is now closed and the winners are Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. 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    2015-02-24 03:25:51


© meritcampus 2019

All Rights Reserved.

Open In App