The Artima Developer Community
Sponsored Link

Java Answers Forum
Help understanding generics

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
maq moh

Posts: 1
Nickname: maqm
Registered: Feb, 2009

Help understanding generics Posted: Feb 14, 2009 9:31 PM
Reply to this message Reply
Advertisement
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,

Topic: Package not found eror Previous Topic   Next Topic Topic: Ask a question for Crystal Report and JSP

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use