Results 1 to 4 of 4
  1. #1
    bunburya is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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:
    Java Code:
    public class CaseCard extends FlashCard // and
    public class StatuteCard extends FlashCard
    // etc
    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.

    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default 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.

  3. #3
    bunburya is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Factory methods and inheriting from an abstract class

    Quote Originally Posted by Junky View Post
    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.
    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Factory methods and inheriting from an abstract class

    I still do not understand your problem without seeing any code.

Similar Threads

  1. Inheriting two kinds of Lists into one Class
    By rsiddharth in forum New To Java
    Replies: 22
    Last Post: 12-26-2010, 03:39 PM
  2. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  3. Q. about abstract factory
    By Algatron in forum Advanced Java
    Replies: 4
    Last Post: 04-07-2009, 02:42 PM
  4. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  5. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •