Menu
Topics Index
...
`

Bounded Types2867


If you need explanation Read this topic

If you need Answer Take test on this topic

Bounded Types2867
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.print();
        obj.sayHello();
    }
}
class Wishes implements Hello, Hi
{
    public void print() {
        System.out.print("Hi...");
    }
    public void sayHello() {
        System.out.print("Hello...");
    }
}
interface Hi
{
    void print();
}

interface Hello
{
    void sayHello();
}

A. Hello...Hi...
B. Hi...Hello...
C. 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.

ans is B  here class wishes implements hello and hi interfaces they are defined in wishes when these are called in boundedtype4 class in void print method these are called with object obj in main function first print() method prints hi... and then sayHello() method prints hello...

Posted by Ramya Reddy Addulla    2014-10-16 03:48:58


Ans is B...here we 2 classes and  2 interfaces..interface is similar type of class which contains only abstract methods...

Here we are dealing wiht generics concept..Class Example<T> means T can any varible..here we can restrict the type by using the Bounded types....meatns T extends A& B...T is able to deal with the child classes of A and B..here A n B can be classes or interfaces...
here T extending Hi and Hello so the T is an varible which can receive only child classes of Hi and Hello...

here we are passing the Whishes class while creating the Object, Whishes is an child class of HI and Hello..
calling the print() and sayHello() corresponding Sop's get printed..

Posted by Uday Kumar    2014-10-16 11:51:46


ans b.
a class can implement more than one interface. by contrast, a class can only inherit a single superclass.

interfaces are syntactically similar to classes, but thy lack instance variable, and their methods are declared without any body.

once it is defined, any number of classes can implement an interface.

by providing interface keyword, java allows u to fully utilize the one interface, multiple methods aspect of polymorphism.

boundtype4 T extends hi and hello interface.T can be any type variable.it can extend ly one class .& symnl used thn T can inherit both.

main class we created reference object for boundtype4 and wishes class is passed as an arg.

obj.print() called thn it invokes print() and sayhello() function .
hi hello get printed.

Posted by Maheshwari Natarajan    2014-10-16 17:15:18


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Ramya Reddy Addulla. 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-10-17 04:02:43


© meritcampus 2019

All Rights Reserved.

Open In App