M
Maqsood Mohammed
Hi there,
I'm new to java generics, and to further understand i wrote a simple
program.. but i think i either got my interface wrong or perhaps dont'
fully understand when to parametrize interfaces.
consider,
interface Dryable<T> {
boolean dry(T obj);
}
interface Washable<T> {
boolean wash(T obj);
}
interface Dryer<T extends Dryable<T>> {
T dryIt(T obj); // return "dried" obj
};
interface Washer<T extends Washable<T>> {
T washIt(T obj); // return "washed" obj
}
// TODO: later change, to accept a list of items..
interface DryerWasher<T extends Washable<T> & Dryable<T>> extends
Dryer<T>, Washer<T> {
boolean cleanIt(T obj);
}
class Apparel implements Washable<Apparel>, Dryable<Apparel> {
String description;
Apparel(String description){
this.description = description;
}
public String toString(){
return description;
}
public boolean wash(Apparel obj){
System.out.println("washing apparel:"+obj);
return true;
}
public boolean dry(Apparel obj){
System.out.println("drying apparel:"+obj);
return true;
}
}
class ApparelDryerWasher implements DryerWasher<Apparel> {
public boolean cleanIt(Apparel obj) {
System.out.println("starting washing");
Apparel w_obj = washIt(obj);
System.out.println("starting drying");
Apparel wd_obj = dryIt(w_obj);
System.out.println("done");
}
public Apparel dryIt(Apparel obj) {
return obj.dry(this);
}
public Apparel washIt(Apparel obj){
return obj.wash(this);
}
}
public static void main(String[] args) {
ApparelDryerWasher apparelCleaner = new ApparelDryerWasher();
apparelCleaner.cleanIt(new Apparel("Jeans-A"));
apparelCleaner.cleanIt(new Apparel("Jeans-B"));
}
Rightaway a compilation-error, stating that ApparelDryerWasher.dry
cannot be applied to ApparelDryerWasher parameter, i understand
passing DryerWasher to "dry" is outrageous, but saying obj.dry() would
mean Dryable interface cannot be parametrized..should it be?
There are plenty things wrong with my design, I'd like to hear what
are the various ways to design/implement this solution..
Thanks,
I'm new to java generics, and to further understand i wrote a simple
program.. but i think i either got my interface wrong or perhaps dont'
fully understand when to parametrize interfaces.
consider,
interface Dryable<T> {
boolean dry(T obj);
}
interface Washable<T> {
boolean wash(T obj);
}
interface Dryer<T extends Dryable<T>> {
T dryIt(T obj); // return "dried" obj
};
interface Washer<T extends Washable<T>> {
T washIt(T obj); // return "washed" obj
}
// TODO: later change, to accept a list of items..
interface DryerWasher<T extends Washable<T> & Dryable<T>> extends
Dryer<T>, Washer<T> {
boolean cleanIt(T obj);
}
class Apparel implements Washable<Apparel>, Dryable<Apparel> {
String description;
Apparel(String description){
this.description = description;
}
public String toString(){
return description;
}
public boolean wash(Apparel obj){
System.out.println("washing apparel:"+obj);
return true;
}
public boolean dry(Apparel obj){
System.out.println("drying apparel:"+obj);
return true;
}
}
class ApparelDryerWasher implements DryerWasher<Apparel> {
public boolean cleanIt(Apparel obj) {
System.out.println("starting washing");
Apparel w_obj = washIt(obj);
System.out.println("starting drying");
Apparel wd_obj = dryIt(w_obj);
System.out.println("done");
}
public Apparel dryIt(Apparel obj) {
return obj.dry(this);
}
public Apparel washIt(Apparel obj){
return obj.wash(this);
}
}
public static void main(String[] args) {
ApparelDryerWasher apparelCleaner = new ApparelDryerWasher();
apparelCleaner.cleanIt(new Apparel("Jeans-A"));
apparelCleaner.cleanIt(new Apparel("Jeans-B"));
}
Rightaway a compilation-error, stating that ApparelDryerWasher.dry
cannot be applied to ApparelDryerWasher parameter, i understand
passing DryerWasher to "dry" is outrageous, but saying obj.dry() would
mean Dryable interface cannot be parametrized..should it be?
There are plenty things wrong with my design, I'd like to hear what
are the various ways to design/implement this solution..
Thanks,