Results 1 to 4 of 4
- 11-22-2011, 04:38 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Factory methods and inheriting from an abstract class
Hi all, first post. I'm currently working on a simple flashcard app to teach myself a bit about Java and it's generally going well, but I've hit a problem.
Basically, my code is structured like this: I have an abstract FlashCard class, and then I have a number of classes inheriting from this class which represent specific types of flashcard, like:
Each type of flashcard has a corresponding display class which creates a UI showing info about the card. So CaseCard has CaseCardDIsplayUI and so on. All these display classes inherit from a single abstract class called CardDIsplayUI. The constructor for each display class takes as an argument an instance of its corresponding flashcard class.Java Code:public class CaseCard extends FlashCard // and public class StatuteCard extends FlashCard // etc
Now, here's my problem: I'm trying to create a factory method which takes one of the classes that inherit from FlashCard, and returns the appropriate display class. And I can't seem to be able to do it properly. Testing with instanceof doesn't work, and overloading a single method doesn't work. The problem is that the compiler thinks, for example, that I am trying to provide an instance of FlashCard to a method that only takes CaseCard, even thought this could never happen in reality.
Does anyone know the best way to proceed here? Sorry for the long post but I couldn't explain my problem in fewer words. Hopefully it's clear enough. I want to create a method that can take any subclass of FlashCard and return its appropriate display class if it has one. I'm having trouble because each display class requires an instance of that particular card class, not a general FlashCard instance.
- 11-22-2011, 04:50 AM #2
Re: Factory methods and inheriting from an abstract class
I don't fully understand your problem nor why you are trying to use a factory method. What I would do is have CaseCardDisplayUI class as an inner class of CaseCard class. That way only CaseCard could create instances of CaseCardDisplayUI.
- 11-22-2011, 05:03 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Re: Factory methods and inheriting from an abstract class
Thanks Junky. The reason I need a factory class (I think) is that each card has different data fields (stored in instance variables). So CaseCard might have "name", "citation", etc, while StatuteCard might have "year", "description" etc. So CaseCardDIsplayUI would take the name and citation from the CaseCard instance it is given, whereas StatuteCardDisplayUI would take the year and description from the StatuteCard instance it is given. The trick is that there's a point in my code where I have an object that could be an instance of any one of these classes, and I need to get the appropriate display class.
Your suggestion would work I think, and it is similar to the solution I was thinking of before I posted this. The main concern I have with an approach like that is that it would blue the line between UI code and backend logic. The CaseCardDisplayUI class controls the user interface, and I was trying to keep that distinct from the FlashCard classes as I've been told in the past that it's good practice to do so. But if this is your solution as well, it may well be the best (only?) approach.
- 11-22-2011, 05:51 AM #4
Similar Threads
-
Inheriting two kinds of Lists into one Class
By rsiddharth in forum New To JavaReplies: 22Last Post: 12-26-2010, 03:39 PM -
How can I call abstract class methods from another class
By srinivas2828 in forum New To JavaReplies: 13Last Post: 03-12-2010, 02:33 PM -
Q. about abstract factory
By Algatron in forum Advanced JavaReplies: 4Last Post: 04-07-2009, 02:42 PM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM -
Abstract Class with Static Methods
By bugger in forum New To JavaReplies: 7Last Post: 09-05-2008, 12:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks