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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:50:06+00:00 2026-06-14T07:50:06+00:00

Since the subclass is not constructed yet, is it unsafe to call an abstract

  • 0

Since the subclass is not constructed yet, is it unsafe to call an abstract method in a super class constructor?

However, if the method’s behaviour does not depend on the constrction of subclass, e.g. just return a constant with regard to the subclass, is it still unsafe or will it work reliably?

Moreover, if it works, how to do it if I do not want to make the super class abstract?

Update: for last question

public class SuperClass {
      public SuperClass() {
          System.out.println(getValue());
      }   

      public String getValue() {
          return "superclass";
      }   

      public static void main(String[] args) {
           new SubClass();
      }

}


class SubClass extends SuperClass {
      public SubClass() {
           super(); // Comment out this or not  will not affect the result
      }   

      public String getValue() {
           return "subclass";
       }   
}

I wrote a test, and figure it out: the result is : subclass
Thanks to @Tim Pote’s example.

  • 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-14T07:50:07+00:00Added an answer on June 14, 2026 at 7:50 am

    It is generally (though not necessarily) considered unsafe. As you said, the superclass may not be fully constructed, and therefore won’t be ready to handle all of the calls a subclass might make in its overridden method.

    However, in the case that all subclasses simply return a constant that isn’t dependent on any other method, then it should be fine. The only downside is that you can’t guarantee that a subclass will override that method in an appropriate manner.

    In regards to your last question: this isn’t an issue of an abstract vs. concrete superclass. This is an issue with calling overridable methods in a constructor. Abstract vs. concrete is beside the point.


    Edit in response to the OP’s comment

    I’m not certain what you mean by “polymorphiscly”. Calling a virtual method always invokes the sub-most implementation. The only time a superclasses implementation is invoked is via the super keyword. For example:

    public class SuperClass {
      public SuperClass() {
        System.out.println(getValue());
      }   
    
      public String getValue() {
        return "superclass";
      }   
    
      public static void main(String[] args) {
        new SubClass();
      }   
    
      public static class SubClass extends SuperClass {
        public String getValue() {
          return "subclass";
        }   
      }   
    }
    

    prints subclass.

    And this:

    public class SuperClass {
      public SuperClass() {
        System.out.println(getValue());
      }   
    
      public String getValue() {
        return "superclass";
      }   
    
      public static void main(String[] args) {
        new SubClass();
      }   
    
      public static class SubClass extends SuperClass {
        public String getValue() {
          return super.getValue() + " subclass";
        }   
      }   
    }
    

    prints superclass subclass

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

Sidebar

Related Questions

Since Intellij does not yet support the Play-Scala-Template-Engine I was thinking about using plain
I have a bunch of classes extending an abstract Base class. Each subclass takes
Basically, what I want to do is force the subclass to call an abstract
The following code does not compile: class OuterClass<T> { class BaseClass { } class
Since every class in java is a subclass of the Object, and variables in
since memcpy should be highly optimized nowadays, does it still make sense to optimize
Since regular jQuery animations are not fluent on iOS ( .hide() , slideDown() ),
I am making a basic graph representation in Scala. abstract class Vertex class Edge
I have an abstract class Vehicle with 2 implemented subclasses RedVehicle and YellowVehicle .
I recently discovered that a method in a derived class can only access the

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.