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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:16:30+00:00 2026-05-28T07:16:30+00:00

Is it possible to call an abstract constructor in one method, then pass that

  • 0

Is it possible to call an abstract constructor in one method, then pass that to some other methods, which would each cast the new object into specific subclasses? That is,

public AbstractClass createNewAbstractClass() {
    //do lots of checks that are the same for each sub class,
    //including geting and checking each variable,
    //and an exception thrown by the constructor.
    AbstractClass abstractClassObject = new AbstractClass(var1, ...);
    return abstractClassObject;
}

public SubClassOne createSubClassOneObject() {
    SubClassOne subClassOneObject = (SubClassOne)createNewAbstractClass(var1,..);
    return subClassOneObject;
}

public SubClassTwo createSubClassTwoObject() { ...

One way to get round this would be to get and check all the variables in one method, then return them in an array, so that the method createSubClassNObject() could use them in the right constructor, but that seems quite messy, and it would mean that each create method would have to check for the same exception in the same way and do the same thing about it, which sounds like exactly the situation you should try to outsource to another method!

I’m interested from a practical point of view – I want my code to be neat and readable – but also from a theoretical point of view – is this actually possible? So even if the answer is no, can you explain why?

  • 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-28T07:16:31+00:00Added an answer on May 28, 2026 at 7:16 am

    There are a couple problems with this:

    • Abstract classes can’t be instantiated; a constructor of an abstract class can only be called in a constructor of a subclass.
    • Even if the parent class wasn’t abstract, the (SubClassOne) cast would fail because we would really have an instance of type AbstractClass, not an instance of type SubClassOne being typed as AbstractClass.

    So unfortunately, the method which returns a SubClassOne will need to call a SubClassOne constructor, which might take the same parameters as AbstractClass and just delegate with a super call.

    I don’t think there’s any easy way around duplicating some exception-handling code in each your factory methods; constructors don’t play well with polymorphism. You could have a central constructor which returns AbstractClass, but it would need a parameter (an enum, a Class, whatever) to tell it which subclass constructor to call.

    abstract class Cow {
      Cow() throws Exception {
        ... // possible exceptions
      }
    }
    
    class FatCow extends Cow {
      FatCow() throws Exception {
        super();
        ...
      }
    }
    
    class GreenCow extends Cow {
      GreenCow() throws Exception {
        super();
        ...
      }
    }
    
    enum CowType {
      FAT_COW, GREEN_COW;
    }
    
    class CowMachine {
      static Cow makeCow(CowType type) {
        try {
          switch (type) {
            case FAT_COW:
              return new FatCow();
            case GREEN_COW:
              return new GreenCow();
            default:
              throw new IllegalArgumentException();
          }
        } catch (Exception e) {
          ...
          return null;
        }
      }
    
      static Cow makeFatCow() {
        return (FatCow) makeCow(CowType.FAT_COW);
      }
    
      static Cow makeGreenCow() {
        return (GreenCow) makeCow(CowType.GREEN_COW);
      }
    }
    

    If you don’t want a big swtich statement, you could instead accept a Class object and call newInstance, although this would be slower.

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

Sidebar

Related Questions

Is it possible to call method of an Abstract class in derived class or
I have an abstract class foo that contains some properties and methods. I have
How does one make a method in an interface that's not abstract? I know
Is it possible to call a constructor from another (within the same class, not
How is it possible to call a client side javascript method after a specific
I want to use/call a method inside EdumateSuperClass that I created in Navigation class.
Possible Duplicate: Why does PHP 5.2+ disallow abstract static class methods? Why can't you
I have one abstract class and two implementations. Let's call them ParentAbstract , ParentA
I'm trying to create an abstract class, defining an abstract method, that is to
How to call base class method if it is not abstract. class WithAbstMethod {

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.