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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:11:10+00:00 2026-05-14T03:11:10+00:00

I need this because the constructor in the superclass is calling a method which

  • 0

I need this because the constructor in the superclass is calling a method which is overridden in the subclass. The method returns a value which is passed to the subclass’ constructor. But the superclass constructor must be called before the subclass constructor, so I have no chance to save the value passed in.

  • 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-14T03:11:10+00:00Added an answer on May 14, 2026 at 3:11 am

    Calling an overridden method from the superclass constructor is simply not going to work – don’t do it. The superclass constructor must always finish before that of the subclass. While the superclass constructor is executing, the object in question is a (half initialized) instance of the superclass, not the subclass! So if you try to call any overridden function from the constructor, the subclass fields it may depend on are not yet initialized (just as you have observed). This is a fundamental fact of class design, and there is no workaround.

    As explained in Effective Java 2nd. Ed. (Chapter 4, Item 17):

    There are […] restrictions that a class must obey to allow
    inheritance. Constructors must not invoke overridable methods, directly
    or indirectly. If you violate this rule, program failure will result. The
    superclass constructor runs before the subclass constructor, so the
    overriding method in the subclass will get invoked before the subclass
    constructor has run. If the overriding method depends on any initialization
    performed by the subclass constructor, the method will not behave as
    expected.

    If you can change the superclass implementation, try moving the call to the virtual function out of the constructor. One way to achieve this is using a Factory Method:

    class Super {
        public void init() { ... }
    }
    
    class Subclass extends Super {
        private Subclass() { super(); ... }
        public void init() { super.init(); ... }
        public static Subclass createInstance() {
            Subclass instance = new Subclass();
            instance.init();
            return instance;
        }
    }
    

    Note that the constructor of Subclass is private to ensure that it can only be instantiated via createInstance(), thus instances are always initialized properly. OTOH this also prevents further subclassing. However, subclassing a concrete class is not recommended anyway – a class meant to be subclassed should be abstract (with a protected constructor in this case). And of course, any further subclasses must also have non-public constructors and static factory methods which diligently call init()…

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

Sidebar

Related Questions

$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have
So from this string: name[id] I need this: id I used str.split ('[]'), but
I need to validate this simple pick list: <select name=<%= key %>> <option value=ETC
Maybe the need to do this is a 'design smell' but thinking about another
In this I need C++ array class template, which is fixed-size, stack-based and doesn't
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
I need to know this since this is a pre-req for .NET 3.5 and
I need to shorten this query and while I'm pretty good at SQL, I'm
I have created an web service I need to publish this service. I need

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.