• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

list interface

 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.");
}
}
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which ? extends you can't add objects to the list
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic