Wednesday 2 October 2013

Generics methods - passing objects and invoking its method

Generics methods - passing objects and invoking its method

I'm trying to understand how Generics works and wrote a method to test it.
I have created Object - Drink and its child object Coffee... and defined a
generic method go1() to invoke the sip() method of both these objects...
I'm running in Eclipse and get an error - stating the method sip() is
undefined for type V.
Can someone explain how this is handled?
class Drink{
public void sip(){
System.out.println("Im Drink method");
}
}
class Coffee extends Drink{
public void sip(){
System.out.println("Im Coffee method");
}
}
public class test{
public static <V> V go1(V o){
o.sip();
}
public static void main(String[] args) {
Drink s1 = new Drink();
go1(s1);
}
}

No comments:

Post a Comment