aspose file tools*
The moose likes Java in General and the fly likes list interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "list interface" Watch "list interface" New topic
Author

list interface

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

why this pogram is giving error i am not able to understand.as per my understanding put interface is put as extends oirrespective of implements while using collections and generics.

package generics;
import java.util.ArrayList;
import java.util.List;
public class Ques04 {
public static void main(String[] args) {
List<? extends Instrument> allInstruments = new ArrayList<Instrument>();
// -->X
allInstruments.add(new Guitar());
allInstruments.add(new Violin());
}
}
interface Instrument {
public void play();
}
class Guitar implements Instrument {
public void play(){
System.out.println("Playing Guitar.");
}
}
class Violin implements Instrument {
public void play(){
System.out.println("Playing Violin.");
}
}

SCJP6.0,My blog Ranchers from Delhi
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
which ? extends you can't add objects to the list
Santhosh Puttu
Greenhorn

Joined: Jun 08, 2010
Posts: 27
you cannot create an object to an interface according to me......
in your program you are creating a object to interface INSTRUMENT type......
also you cannot extend interface you can only implement it.....
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Santhosh Puttaswamy wrote:you cannot create an object to an interface according to me......
in your program you are creating a object to interface INSTRUMENT type......
also you cannot extend interface you can only implement it.....


hi ,

i am 100% sure that in case of making list we have to use extends instead of implements............
like while creating a list of any class which implements serialization we
use

List<? extends serializable> llist=new ArrayList(serializable);
This is correct.........but why above program is giving exception.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

See the generics tutorial, specifically this part:
There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into shapes in the body of the method. For instance, this is not allowed:You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 48
The way generics works is by a method called erasure
At compile time the compiler inserts type casts to check for the type.
At run time there is no difference between List<Object> & List

hence my thought is cannot be type casted to Instrument as Guitar class can have additional methods not defined by Instrument
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Rajesh: Are you sure? Wouldn't that defeat almost everything about polymorphism? Try this code:
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 48
David Newton wrote:@Rajesh: Are you sure? Wouldn't that defeat almost everything about polymorphism? Try this code:



Polymorphism is a totally different thing, in generics it is different because in generics the pupose of type safety is defeated if it cannot enure the type safety. In Polymorphism you can use an explicit type cast to ensure type safety

Hence generics are not co-variant as in the case of arrays

Correct me if I am wrong

An extract from the link http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html



You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Rajesh wrote:Correct me if I am wrong

Okay.

You said:
Rajesh wrote: hence my thought is cannot be type casted to Instrument as Guitar class can have additional methods not defined by Instrument

Which would defeat polymorphism. Of course Guitar can be cast to Instrument, as the code I posted showed, and as intuition should imply--Guitar implements Instrument: a guitar is-a instrument.

The issue isn't with a cast, the issue is with generics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: list interface
 
Similar Threads
mock exam question
Generics Issue
Regarding Generics !
Generics Prob
Generics for Interface