?
Solved

Abstract class problems

Posted on 2006-04-01
19
Medium Priority
?
225 Views
Last Modified: 2010-03-31
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



0
Comment
Question by:tiger0516
  • 8
  • 6
  • 5
19 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 16348931
You have the wrong parameter to your methods. SHould be of type Object not File
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16348940
Use Object? Then I have to use something like File aFile=(File) Object, right?

0
 
LVL 1

Author Comment

by:tiger0516
ID: 16348943
     protected ImageIcon createImage(Object o)
      {
            File imgSrc=(File)o;
            return (new  ImageIcon(imgSrc.getPath()));
      }

right?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16348948
Yep
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16348958
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16348959
You could do, yes
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16348962
Is this the only way, or there are better solutions?

Thanks
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1600 total points
ID: 16348965
Can't think of another better one at the moment
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16348967
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16348976
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16348983
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
 
LVL 92

Expert Comment

by:objects
ID: 16349003
> public abstract class ImageCache<T> {

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

Author Comment

by:tiger0516
ID: 16349008
>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
 
LVL 92

Expert Comment

by:objects
ID: 16349026
If you don't have a need then get rid of it :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16349044
>>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
 
LVL 92

Expert Comment

by:objects
ID: 16349049
Above you stated using instanceof was the best way to go, which is not correct.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16349067
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
 
LVL 92

Expert Comment

by:objects
ID: 16349088
And you suggested using that :) sorry, but its just poor advice.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16349106
Perhaps you don't understand my comments. tiger0516, let me know if you need clarification
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question