import java.util.*;
class Fruit implements Eatable {
public String toString() { return "Fruit";}
}
class Apple extends Fruit { public String toString() { return "Apple";}}
class Grape extends Fruit { public String toString() { return "Grape";}}
class Toy { public String toString() { return "Toy" ;}}
interface Eatable {}
public class Try {
public static void main(String[] args) {
Box b = null;
Box<String> bStr = null;
b = (Box)bStr; // 가능은 하지만 경고 Box<String> -> Box
bStr = (Box<String>)b; // 가능은 하지만 경고 Box -> Box<String>
}
}
class FruitBox<T extends Fruit & Eatable> extends Box<T> {}
class Box<T> {
ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item); }
T get(int i) { return list.get(i); }
int size() { return list.size(); }
public String toString() { return list.toString();}
}
import java.util.*;
class Fruit implements Eatable {
public String toString() { return "Fruit";}
}
class Apple extends Fruit { public String toString() { return "Apple";}}
class Grape extends Fruit { public String toString() { return "Grape";}}
class Toy { public String toString() { return "Toy" ;}}
interface Eatable {}
public class Try {
public static void main(String[] args) {
FruitBox<? extends Fruit> fbox = (FruitBox<? extends Fruit>)new FruitBox<Fruit>();
// 위 생성자 부분에 괄호부분 (FruitBox<? extends Fruit>) 은 생략가능 하다.
// 원래는 들어가야한다. 아래코드는 생략된 코드이다.
// FruitBox<Apple> -> FruitBox<? extends Fruit>
FruitBox<? extends Apple> abox = new FruitBox<Apple>();
// FruitBox<? extends Fruit> -> FruitBox<Apple> 가능 ?
FruitBox<Apple> appleBox = (FruitBox<Apple>)abox; // 가능. but 경고발생
// why? 오른쪽부분은 와일드카드로 선언되어있는데 왼쪽에 명확한 타입으로 바꿀려고 하니
// 경고가 발생하는 것
}
}
class FruitBox<T extends Fruit & Eatable> extends Box<T> {}
class Box<T> {
ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item); }
T get(int i) { return list.get(i); }
int size() { return list.size(); }
public String toString() { return list.toString();}
}