Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Type mismatch assigning factory class result to subclass

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Type mismatch assigning factory class result to subclass

    I have a base class with a factory method as follows. It looks for a database record and returns it if found, or creates it if not.

    public  class Animal {
    	public static Animal factory(String id) {
                   Animal a = Database.get(id);
                   if (a==null) 
                       a = Database.make(id);
                   return a;
    	}
    }

    I have a bunch of subclasses eg:

    public class Panda extends Animal {
    // etc etc
    }

    And I want to be able to use the parent's factory method to make basic instances of these subclasses, but of course I get a "Type mismatch: cannot convert from Animal to Panda"

    public class test {
    	public static void main(String[] args) {
    		Panda p = Panda.factory("panda123");
    	}
    }

    It seems I have to repeat the code of the parent factory method (which is more complex than I show above) in all the subclasses, otherwise maybe I'm missing something in the use of factory classes in cases like this?

    Any advice much appreciated!

    thanks,


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Type mismatch assigning factory class result to subclass

    Hello.
    Just try this and let me know.
    Panda p = (Panda)Panda.factory("panda123");

    Thanks,
    Syed.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Type mismatch assigning factory class result to subclass

    Thanks Syed, but this gives a runtime class cast error...

    Exception in thread "main" java.lang.ClassCastException: test.Animal cannot be cast to test.Panda
    at test.test.main(test.java:7)


    Quote Originally Posted by syedbhai View Post
    Hello.
    Just try this and let me know.
    Panda p = (Panda)Panda.factory("panda123");

    Thanks,
    Syed.

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Type mismatch assigning factory class result to subclass

    I see.
    Then I think you have some extra variables or methods in the subclasses. Because of that it will throw class cast exception.
    Then you will have to do it this way.
    Make the method in the superclass as abstract. Provide the specific implemention in every subclass. When writing to database create an object of desired subclass and invoke the method w.r.t to that object.
    Let me know if you have understood what I am saying.

    Thanks,
    Syed.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Type mismatch assigning factory class result to subclass

    Casting to a Panda will not work. What happens if the factory method returns a Monkey object? Obviously it cannot be cast to a Panda. Since a parent class has no idea about what subclasses there are this is not possible. You might want to rethink your design.
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    roger1990 (July 28th, 2013)

  7. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Type mismatch assigning factory class result to subclass

    Quote Originally Posted by Junky View Post
    Casting to a Panda will not work. What happens if the factory method returns a Monkey object? Obviously it cannot be cast to a Panda. Since a parent class has no idea about what subclasses there are this is not possible. You might want to rethink your design.
    Thanks Junky, and sorry if it was I who made you grumpy! :-)

    I guess I expected that Java would instantiate a Panda, eg using the default no-arg constructor (which is provided) to allow the cast to happen. I also would have expected a run-time error where the cast is not up or down the class hierarchy (as in the cast from monkey to panda). But, I'm just showing my ignorance again i guess!

    OK so I have a new approach, with a factory class that takes a generic:

    public class AnimalFactory <T extends Animal> {
     
    	private Class<T> thisClass;
     
    	public AnimalFactory(Class<T> c) {
    		thisClass = c;
    	}
    	public T make() throws Exception {
    		final T animal;
    		animal = thisClass.newInstance();
    		return animal;
    	}	
    }

    Then I can do something like

    		AnimalFactory<Panda> pandaFactory = new AnimalFactory<Panda>(Panda.class);
    		Panda p = pandaFactory.make();

    Does that look like a more standard approach?

    It works so far, but I find that I'll need to use some reflection if I want to actually do other things with the class I am making. Since java doesn't allow me to access methods or variables of "T" directly...

Similar Threads

  1. Help understanding the result of a type int variable
    By Abadude in forum Object Oriented Programming
    Replies: 3
    Last Post: December 11th, 2012, 08:38 AM
  2. Type mismatch: cannot convert from void to ClassX.MethodX
    By RevMoses77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 12:05 PM
  3. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 11:17 AM
  4. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 04:26 PM