File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

list interface

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.");
}
}
 
Manish Singh
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which ? extends you can't add objects to the list
 
Santhosh Puttu
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rajesh: Are you sure? Wouldn't that defeat almost everything about polymorphism? Try this code:
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic