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 6048381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:27:25+00:00 2026-05-23T07:27:25+00:00

I have a library project which contains an abstract class, let’s say ClassA .

  • 0

I have a library project which contains an abstract class, let’s say ClassA. In the project that uses that library project, I have ClassB that extends ClassA.

Now here’s my problem. In the library project I want to use an instance of the implemented ClassB, but I have no idea how to retrieve that instance. Is there any pattern or other ideas for this?

Here’s a simple diagram of the situation.

Edit

The reason I’m asking is that I’m creating multiple applications, which only have different methods in ClassB. Therefore I’m creating a library that all of these applications can use, only having to extend ClassA. These applications are separate projects, using the library.

  • 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-05-23T07:27:25+00:00Added an answer on May 23, 2026 at 7:27 am

    What you’re looking for is something like the Abstract Factory pattern. The application code (the code that calls into the library) would, at some point, need to pass in a Factory class that would be used by the library to create instances of ClassA objects. In your case, the Factory class would generate instances of ClassB.

    Depending on the design and functionality of the classes, it’s likely that ClassA should be redesigned as an interface, or at least as an abstract class, as part of this refactoring.

    EDIT:
    Here’s an untested pseudo example:

    This code would be in the library

    class INeedAFactory {
        public Interface MyFactory {
            public ClassA makeClassAInstance();
        }
    
        private MyFactory m_factory;
        public registerFactory(MyFactory factory) {
            m_factory = factory;
        }
    
        private ClassA makeClassAInstance() {
            // m_factory had better not be null!
            return m_factory.makeClassAInstance();
        }
    
        private void ClassAConsumer() {
            ClassA classA = makeClassAInstance();
            // ... etc. ...
        }
    }         
    

    This code would be in the application:

    class LibraryPatron {
        class MakeClassB implements INeedAFactory.MyFactory {
            public ClassA makeClassAInstance() {
                return new ClassB();
            }
        }
        public LibraryPatron() {
            INeedAFactory libraryObject = new INeedAFactory();
            libraryObject.registerFactory(new MakeClassB());
            // ... etc...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.