Current location: Hot Scripts Forums » Programming Languages » Everything Java » conceptual problem: abstract class, interface or class


conceptual problem: abstract class, interface or class

Reply
  #1 (permalink)  
Old 05-10-07, 06:07 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
conceptual problem: abstract class, interface or class

Hi
I created a class named ID3Frame, and it has some subclass, such as ID3TextFrame and ID3ImageFrame. One of the methods that need to be implemented (or extended) in each subclass is the "getFrameValue" method.

Now here's my problem: when a ID3TextFrame object is used, the getFrameValue should return a String, but when an ID3ImageFrame is used, it should return an Image.
I know i can just extend the ID3Frame class, and add the method, but then i have to cast each ID3Frame object (i have a HashMap where i store ID3Frame objects in) to the specified subclass before calling methods (cause the getFrameValue is not defined in the ID3Frame class). When i use an abstract ID3Frame class, i can only return an Object, and the same goes for an interface.
At the moment these are my classes:
Java Code:
  1. public abstract class ID3Frame {
  2.  
  3.   public abstract Object getFrameValue();
  4. }
  5.  
  6. public class ID3ImageFrame extends ID3Frame {
  7.   public Image getID3FrameValue(){
  8.     return new Image();
  9.   }
  10. }
  11.  
  12. public class ID3TextFrame extends ID3Frame {
  13.   public String getFrameValue(){
  14.     return new String();
  15.   }
  16. }
this works, but then i always get an Object as return type, and not the correct one

So my question is: is there a way to specify the method in the ID3Frame class, with the method returning an Image, String or Integer object depending on the subclass?

I hope i explained it well enough, if not let me know, i'll give it another try
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-11-07, 05:49 PM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
You should be receiving the correct return value in ID3TextFrame, but ID3ImageFrame shouldn't even compile since the method name isn't the same as it's extended class method. For instance, you could test with this code:

Code:
public class ID3ImageFrame extends ID3Frame {
  public String getFrameValue(){
    return "Here";
  }
}
and calling getFrameValue gives you the correct string "here". I don't know why you be getting different return values, since there are no overloaded implementations using pure Object. Maybe show me your calling code?
__________________
my site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-12-07, 08:30 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by King Coder View Post
You should be receiving the correct return value in ID3TextFrame, but ID3ImageFrame shouldn't even compile since the method name isn't the same as it's extended class method.
just a typo, it is the same method in my ID3ImageFrame class (getFrameValue)

Quote:
Originally Posted by King Coder View Post
and calling getFrameValue gives you the correct string "here". I don't know why you be getting different return values, since there are no overloaded implementations using pure Object. Maybe show me your calling code?
It returns the value just as expected, but i was wondering if it's possible to retrieve the correct object, and not Object (this sounds stupid). Here's an example:
Java Code:
  1. public class Test{
  2.  
  3.   public static HashMap<String, ID3Frame> frames;
  4.  
  5.   public Test(){
  6.     ID3Frame img_frame = new ID3ImageFrame();
  7.     // this method exists in the ID3Frame class as well: 
  8.     img_frame.setValue("images/test.jpg");
  9.     ID3Frame text_frame = new ID3TextFrame();
  10.     text_frame.setValue("This is a text frame");
  11.  
  12.     // this is how i store my ID3Frames:
  13.     frames = new HashMap<String, ID3Frame>();
  14.     frames.put("IMAGE", img_frame);
  15.     frames.put("TEXT", text_frame);
  16.   }
  17.  
  18.   public static Image getImage(){
  19.     if(frames.containsKey('IMAGE')) {
  20.       return frames.get('IMAGE').getFrameValue(); // so this one should return an Image
  21.     }
  22.     return null;
  23.   }
  24.  
  25.   public static String getText(){
  26.     if(frames.containsKey('TEXT')) {
  27.       return frames.get('TEXT').getFrameValue(); // this should return a String
  28.     }
  29.     return null;
  30.   }
  31.  
  32.   public static void main(String[] args){
  33.     Test t = new Test();
  34.     Image img = t.getImage();
  35.     String text = t.getText();
  36.   }

So i was wondering if it was possible to do it like this: just call the method getFrameValue, and depending on what type of ID3Frame (ID3TextFrame, Id3ImageFrame), an Image object or a String object would be returned

Thanks for looking into it

EDIT: forget about it. I'm using these methods as well, so i can cast the returned type to Image, String or Integer, and use the abstract method (public abstract Object getFrameValue)i defined in my first post.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need to pass this class Please Help! Negative6 Everything Java 1 05-03-07 07:55 AM


All times are GMT -5. The time now is 10:55 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.