Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:16:17+00:00 2026-06-15T15:16:17+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:16:18+00:00Added an answer on June 15, 2026 at 3:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with a line of code like: $user->has('roles', ORM::factory('role', array('name' =>
I was wondering if this is an 'unwholesome' use of a factory pattern, or
I'm having the following problem when using GWT and request factory. I am working
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I am having problem to get a numerical value for this expression where I
I'm having problem with datagrid view. I have attached an image with the code
I am having problem with drawing multiple lines during repaint. The code is as
I'm having a problem testing the following model: class Bill < ActiveRecord::Base belongs_to :consignee
I'm having trouble implementing the Factory Design Pattern. I've looked at http://dotnet.dzone.com/articles/design-patterns-c-factory , which
I am having a problem while persisting a class. I have a class called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.