Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

Abstract class problems

I have an abstract class, ImageCache. There are two abstract methods in it:      

public abstract class ImageCache<T> {

... ...

//constructor
public ImageCache(int capacity)
{
... ...
}

//abstract methods
      protected abstract ImageIcon createImage(T imgSrc);
      protected abstract void imageLoaded(T imgSrc);

}


I tried this but does not work:

public class LoadedCache<T> extends ImageCache {
ImageIcon imageIcon;
      
      public LoadedCache (int i)
      {
            super(i);
      }
... ...
      
      protected ImageIcon createImage(File imgSrc)
      {
            return (new  ImageIcon(imgSrc.getPath()));
      }
      
      
      protected  void imageLoaded(File imgSrc)
      {
            //some operations
      }
}

Error:
1.The type LoadedCache<E> must implement the inherited abstract method ImageCache.createImage(Object)
2.The type LoadedCache<E> must implement the inherited abstract method ImageCache.imageLoaded(Object)

I think it might be an easy question for gurus, but, it is really a question for me //blush



ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tiger0516
tiger0516

ASKER

Use Object? Then I have to use something like File aFile=(File) Object, right?

     protected ImageIcon createImage(Object o)
      {
            File imgSrc=(File)o;
            return (new  ImageIcon(imgSrc.getPath()));
      }

right?
Yep
If the parameter's type is Object, when I want to create an ImageIcon, I sometimes will use File as parameter, sometimes will use Photo (another class) as parameter, sometimes will use URL as parameter.

How can I deal with so many cases? Use 'instanceof'?

Thanks
You could do, yes
Is this the only way, or there are better solutions?

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
:-)
If you wantt to create images from different sources then you'd have different methods, in your interface and your implementation.
Using instanceof is messy.

> public abstract class ImageCache<T> {

and you're not even using T so you might as well get rid of it.
>If you wantt to create images from different sources then you'd have different methods, in your interface and your implementation.Using instanceof is messy.

I was also told so. Thanks.

> public abstract class ImageCache<T> {

>and you're not even using T so you might as well get rid of it.

How shall I use that? I think I need to have a new question for this. But what's the question's title shall be?

Thanks
If you don't have a need then get rid of it :)
>>I was also told so. Thanks.

You can of course add whatever methods you like, but you need to honour the interface (abstract method) of createImage(Object o) and that includes honouring for clients other than your own code. Since that probably won't actually be called with a parameter of type Object, you'd need to type-check and cast appropriately anyway
Above you stated using instanceof was the best way to go, which is not correct.
If createImage(Object o) is being used, its use of instanceof is practicably unavoidable. If it isn't, get rid of it if you can
And you suggested using that :) sorry, but its just poor advice.
Perhaps you don't understand my comments. tiger0516, let me know if you need clarification