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



LVL 1
tiger0516Asked:
Who is Participating?
 
CEHJCommented:
You have the wrong parameter to your methods. SHould be of type Object not File
0
 
tiger0516Author Commented:
Use Object? Then I have to use something like File aFile=(File) Object, right?

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

right?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
Yep
0
 
tiger0516Author Commented:
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
0
 
CEHJCommented:
You could do, yes
0
 
tiger0516Author Commented:
Is this the only way, or there are better solutions?

Thanks
0
 
CEHJCommented:
Can't think of another better one at the moment
0
 
tiger0516Author Commented:
Thanks
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
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.

0
 
objectsCommented:
> public abstract class ImageCache<T> {

and you're not even using T so you might as well get rid of it.
0
 
tiger0516Author Commented:
>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
0
 
objectsCommented:
If you don't have a need then get rid of it :)
0
 
CEHJCommented:
>>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
0
 
objectsCommented:
Above you stated using instanceof was the best way to go, which is not correct.
0
 
CEHJCommented:
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
0
 
objectsCommented:
And you suggested using that :) sorry, but its just poor advice.
0
 
CEHJCommented:
Perhaps you don't understand my comments. tiger0516, let me know if you need clarification
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.