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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You have the wrong parameter to your methods. SHould be of type Object not File

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tiger0516Author Commented:
Use Object? Then I have to use something like File aFile=(File) Object, right?

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

right?
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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

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

Mick BarryJava DeveloperCommented:
> public abstract class ImageCache<T> {

and you're not even using T so you might as well get rid of it.
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
Mick BarryJava DeveloperCommented:
If you don't have a need then get rid of it :)
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
Mick BarryJava DeveloperCommented:
Above you stated using instanceof was the best way to go, which is not correct.
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
Mick BarryJava DeveloperCommented:
And you suggested using that :) sorry, but its just poor advice.
CEHJCommented:
Perhaps you don't understand my comments. tiger0516, let me know if you need clarification
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.