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

  • Home
  • SEARCH
  • 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 7868591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:03:07+00:00 2026-06-03T01:03:07+00:00

Possible Duplicate: Regarding factory design pattern through reflection I was doing R&D on factory

  • 0

Possible Duplicate:
Regarding factory design pattern through reflection

I was doing R&D on factory pattern I have developed the below code. Right now I know the subclasses are Dog and Cat, but please advise me. What to do if I want to achieve the same thing through reflection by passing the class name in main.java?

Animal

public abstract class Animal {
    public abstract String makeSound();
}

Dog

public class Dog extends Animal {

    @Override
    public String makeSound() {
        return "Woof";
    }
}

Cat

public class Cat extends Animal {

    @Override
    public String makeSound() {
        return "Meow";
    }
}

AnimalFactory

public class AnimalFactory {

    public Animal getAnimal(String type) {
        if ("canine".equals(type)) {
            return new Dog();
        } else {
            return new Cat();
        }
    }
}

Main

public class Main {

    public static void main(String[] args) {
        AnimalFactory animalFactory = new AnimalFactory();

        Animal a1 = animalFactory.getAnimal("feline");
        System.out.println("a1 sound: " + a1.makeSound());

        Animal a2 = animalFactory.getAnimal("canine");
        System.out.println("a2 sound: " + a2.makeSound());
    }
}

Please advise it how I can add reflection functionality into it so that I don’t need to even determine the type, just pass the class name in the main java and object of that subclass gets created.

  • 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-03T01:03:09+00:00Added an answer on June 3, 2026 at 1:03 am

    If you pass the fullyqualified name of the class, you can instantiate them as following:

    return (Animal) Class.forName(fullyQualifiedClassName).newInstance();
    

    To avoid ClassCastException, you could test that the returned class of Class.forName() is indeed a subclass of Animal before invoking newInstance(). Use isAssignableFrom for that.

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

Sidebar

Related Questions

Possible Duplicate: void * arithmetic Hi guys I have a small question regarding pointer
Possible Duplicate: Benefits of inline functions in C++? I have a confusion regarding the
Possible Duplicate: Finally Block Not Running?? I have a question regarding finally block in
Possible Duplicate: Design question regarding Java EE entity with multiple language support I'm working
Possible Duplicate: Can someone explain C++ Virtual Methods? I have a question regarding to
Possible Duplicate: Best practices regarding equals: to overload or not to overload? Does anyone
Possible Duplicate: Naming Boolean columns in Rails What is the rails convention regarding names
Possible Duplicate: SendInput and 64bits I'm using SendInput from .NET code (PInvoke). code used
Possible Duplicate: Why does InputStream#read() return an int and not a byte? Issue regarding
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to

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.