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.");
}
}
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.....
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.
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.
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: 41
posted
0
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
@Rajesh: Are you sure? Wouldn't that defeat almost everything about polymorphism? Try this code:
Rajesh Nagaraju
Ranch Hand
Joined: Nov 27, 2003
Posts: 41
posted
0
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
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 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.