Menu
Topics Index
...
`

Bounded Types2865


If you need explanation Read this topic

If you need Answer Take test on this topic

Bounded Types2865
What will be the output of the following program?
public class BoundedTypeDemo4 {
    
    public static void main(String args[])
    {
        BoundedType4<Wishes> obj = new BoundedType4<Wishes>(new Wishes());
        obj.print();
    }
}
class BoundedType4 <T extends Hi & Hello>
{
    T obj;
    BoundedType4(T obj)
    {
        this.obj = obj;
    }
    void print()
    {
        obj.sayHello();
        obj.print();
    }
}
class Wishes extends Hello implements Hi
{
    public void print() {
        System.out.print("Hi");
    }
}
interface Hi
{
    void print();
}

class Hello
{
    void sayHello()
    {
        System.out.print("Hello   ");
    }
}

A. Hi
B. Hello
C. Hello   Hi
D. Compilation Error
E. Runtime Error
Topic: Java Bounded Type - Bounded Type 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.

a

Posted by Vikas Tiyagi    2014-11-05 12:13:24


ans is D as here hi is a interface and hello is a class . class boundedType4 <T extends Hi & Hello> here  class cannot extends a interface and a class it cant be possible either both should be interfaces or both should be classes

Posted by Ramya Reddy Addulla    2014-11-05 12:19:54


Ans is D...here we have 3 classes and 1 interface...here we use the concept of the Boundedtypes which will restrict the Objects creation..here the class BoundedType4 we can create objects which are child classes of Hello and Hi only

while using the MultipleBoundedtype one rule we have that it can extends any num of interfaces..if we have multiple bounds with single class means than class must be the 1st...here we have interface is there so compiler wil raise an error..
Hello & Hi than it wil Be ok..

Posted by Uday Kumar    2014-11-05 16:28:23


ans d. in this program we have 4 classes and 1 interface..by using reference obj print () method called.coming to class wishes it extends hello class and implements hi interface.by using extend keyword we can ly inherit 1 class features, by using implements keyword we can use more than 1 interface.thn coming to the boundedtype4 class here we try to inherite features hello class and hi interface. so compilr throws error.extends for inherit a class,use implements keyword for interface.

Posted by Maheshwari Natarajan    2014-11-05 18:44:33


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Vikas Tiyagi. 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-06 03:08:23


© meritcampus 2019

All Rights Reserved.

Open In App