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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:00:48+00:00 2026-05-15T04:00:48+00:00

In Java, is there any way to create an instance of any class that

  • 0

In Java, is there any way to create an instance of any class that extends abstract class A, inside a member method of class A? Classes that extends abstract class A will return their instances with this method, but i don’t want to implement same method in all subclasses with a “return this();” kind line.

EDIT: Ok sorry for the short explanation. In my application, there is an interface called Application, and it has a getInstance() method that returns an Application type. There is an abstract class called AbstractApplication which is a convenience class for Application interface for implementation, but only the interface is exposed in other apps. In some other apps, there will be a lookup for application objects, and this lookup will return an Application type (interface), not a specific implementation. Now here’s my question; is there a way to implement getInstance() in AbstractApplication class so subclasses does not need to implement this method?

  • 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-15T04:00:48+00:00Added an answer on May 15, 2026 at 4:00 am

    Yeap. That’s pretty easy ( unless I misunderstood )

    You have to use the Prototype design pattern ( or a variation of it that I show here )

    It is useful when you don’t know what the factory class could be until runtime. Unlike AbstractFactory where you can have different subclasses creating new types but you may pick one based on certain conditions.

    With prototype you may simple get the “original” object ( the prototype ) injected into your application ( by virtue of a full futured Dependency Injection framework or by a simple class name ) and then create new instances of it.

    Here’s a sample code showing how to do this with a variation ( not using clone but newInstance method )

    public abstract class Application {
        public Application newInstance() {
            try {
                return this.getClass().newInstance();//this creates an instance of the subclass 
            } catch( InstantiationException ie ){
                throw new RuntimeException( ie );
            } catch( IllegalAccessException iae ){
                throw new RuntimeException( iae );
            }
        }
        public String toString() {
            return "This is the class named: \""+ this.getClass().getSimpleName()+"\"";
        }
    } 
    // subclasses doesn't repeat the creation part
    // they just have to override specific methods. 
    class FirstClass extends Application {}
    class SecondClass extends Application {}
    class ThirdClass extends Application {}
    

    And the rest of your code may program to the Application interface:

    public void doSomethingWith( Application application ) {
            System.out.println( application.toString() );
    }
    public void startApplication( Application app ) {
        // etc etc 
    }
    

    Whenever you need a new instance you just call:

    Application newApp = original.newInstance();
    

    And that would create the correct type.

    As you see the subclasses doesn’t specify how to create new subclasses, that’s all in the base class.

    Invoking the method newInstance will always create a new instance which of the same type.

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

Sidebar

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.