Menu
Topics Index
...
`

How Generics Improve Type Safety


If you need explanation Read this topic

If you need Answer Take test on this topic

How Generics Improve Type Safety
What will be the output of the following program?
public class GenericTypeSafetyDemo {
    public static void main(String args[])
    {
        NonGeneric obj1 = new NonGeneric(88);
        obj1.showType();
        NonGeneric obj2 = new NonGeneric("MeritCampus");
        obj2.showType();
    }
}
class NonGeneric
{
    Object ob;
    NonGeneric(Object ob)
    {
        this.ob = ob;
    }
    void showType()
    {
        System.out.println("Type is : "+ob.getClass().getName());
    }
}

A. Type is : 88
Type is : MeritCampus
B. Type is : java.lang.Integer
Type is : java.lang.String
C. Compilation Error
D. Runtime Error
Topic: How Generics Improve Type Safety In Java

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.

Posted by Gopinath Manchikanti    2014-11-11 12:36:40


Ans is B..We can write an Method r Constructor which can receive any type of data without using the Generics by using the Object class which is Parent class for all classes in Java..Some times we need explict conversion when we use this mechanisam...

here we creata Object for  NonGeneric class by passing the Integer value here int wil convert to Interger and pointedd by the Object class Reference...In Java Parent class Reference can refer Child class Object...

next we are calling the showType() by using this Object...in this methd we are calling getClass().getName() this wil return the class name at runtime which the Object is pointign...

here we creata Object for  NonGeneric class by passing the String   pointedd by the Object class Reference...In Java Parent class Reference can refer Child class Object...
Which is Integer In Lang package
next we are calling the showType() by using this Object...in this methd we are calling getClass().getName() this wil return the class name at runtime which the Object is pointign...
Which is String in Lang Package

Posted by Uday Kumar    2014-11-11 12:40:56


b

Posted by Shubham Bansal    2014-11-11 13:12:22


ans b.The getClass() method of Object returns the Class object that describes the class of this object. This method
is final, so it cannot be overridden by subclasses.Object. getClass() returns the
Class object associated with any Object,getName() returns the name of the class.The getName() method of a special Class object returns a string that
contains the name of the primitive type represented by the object.                                                 in this program 2 objects created one with integer and other with String.                                                 by using 1st obj reference showtype() called here object is Integer ,ob.getclass() returns integer class object thn getName() returns Name of the class.so java.lang.Integer get printd.                                                                                 similary by using 2nd obj refernce String object is passed here class is String class ,so ob.getClass().getName() returns java.lang.String

Posted by Maheshwari Natarajan    2014-11-11 16:38:31


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-12 04:40:05


© meritcampus 2019

All Rights Reserved.

Open In App