1

I am having a problem with Factory pattern when it use with Inheritance,

This is my code

public class Animal {
  public int numberOfLegs() { return 2 ;}
}

public class Cat extends Animal {
  public String getSound() {return "Maaaw";}
}
public class Dog extends Animal {
  public String getSound() {return "woof";}
}

public class AnimalFactory {
  public Animal getAnimal(String name){
    Animal an= null ;
    if(name=="cat"){an = new Cat();}
    else if(name=="dog"){an=new Dog();}
    return an ;
  }
}

public class FactoryDemo {

  public static void main(String[] args) {
    AnimalFactory anmF=new AnimalFactory();
    Animal anm=anmF.getAnimal("cat") ;
    System.out.println("legs : "+anm.numberOfLegs()); // working fine
    System.out.println("sound : "+anm.getSound());    // giving error
  }
}

When I run this, I can't go to the getSound() method. It giving a error.

This'll work fine if I define the Animal class as Abstract class,
But I want to how to deal Factory pattern such a situation like this.

2
  • It helps to know what error you get. Please include it in your question. Commented Dec 8, 2012 at 13:31
  • 1
    If you think all animals should have a sound, then the getSound() method should be part of the Animal class. It doesn't have anything to do with the factory pattern. Commented Dec 8, 2012 at 13:32

3 Answers 3

8

You need to add an abstract method for getSound

public abstract class Animal {
    public int numberOfLegs() { return 2 ;}
    public abstract String getSound();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Yes it solved the issue, but i want to know is there any way to do this without define Animal class as Abstract ?
You would need to provide a default implementation of getSound().
2

Change your code to:

public abstract class Animal {
    public int numberOfLegs() { 
        return 2;
    }

    public abstract String getSound();
}


public class Cat extends Animal {
    public String getSound() {
        return "Maaaw";
    }
}

public class Dog extends Animal {
    public String getSound() {
        return "woof";
    }
}

public class AnimalFactory {
    public Animal getAnimal(String name) {

        Animal an = null;
        if ("cat".equals(name)) {
            an = new Cat();
        } else if ("dog".equals(name)) {
            an = new Dog();
        } 
        return an;
     }
}

You shall add abstract method, and use equals in your factory method instead of using == on Objects.

3 Comments

Thanks, Yes it solved the issue, but i want to know is there any way to do this without define Animal class as Abstract ?
Of course. Just add an implementation for getSound in animal and remove the abstract keyword.
Great explanation , but this is an example of Simple Factory pattern which is usually not considered as an design pattern. It would be great to use Factory method design pattern when it comes to java inheritance as it would safeguard us to avoid any existing code change when new classes will be added in the hierarchy.
1

The code you included, is nothing like a Factory anything. If you refer to the Factory Method Pattern then, what you implemented as part of your OP is an incorrect implementation. There are two "Factory" code designs, one is the Factory Method Pattern I indicated before (your code is definitely not that) and the Factory recommended in the Effective Java book, which is the design of choice for the Java JDK i.e. the valueOf or create* methods.

1 Comment

A bit late to the party... @SkyWalker why do you think it is not nothing like Factory anything? Isn't this exactly Factory Method Pattern if AnimalFactory would implement a AnimalFactory interface?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.