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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:46:49+00:00 2026-05-16T00:46:49+00:00

I have something like this: public abstract class Menu { public Menu() { init();

  • 0

I have something like this:

    public abstract class Menu {
     public Menu() {
      init();
     }

     protected abstract void init();

     protected void addMenuItem(MenuItem menuItem) {
      // some code...
     }
    }

    public class ConcreteMenu extends Menu {
     protected void init() {
      addMenuItem(new MenuItem("ITEM1"));
      addMenuItem(new MenuItem("ITEM2"));
      // ....
     }
    }

//Somewhere in code
Menu menu1 = new ConcreteMenu();

As you can see superclass’s init method is abstract and is called by constructor automatically after object is created.

I’m curious if i can run into some sort of problems with code like this, when i need to create some object of this kind whose structure wont’t be changed in time.

Would be any approach better? It works in Java, but will it work in C++ and possibly ActionScript?

Thank you for answer.

  • 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-16T00:46:50+00:00Added an answer on May 16, 2026 at 12:46 am

    DO NOT INVOKE OVERRIDEABLE METHODS FROM THE CONSTRUCTOR.

    A quote from Effective Java 2nd Edition, Item 17: Design and document for inheritance, or else prohibit it:

    There are a few more 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 be 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.

    Here’s an example to illustrate:

    public class ConstructorCallsOverride {
        public static void main(String[] args) {
            abstract class Base {
                Base() { overrideMe(); }
                abstract void overrideMe(); 
            }
            class Child extends Base {
                final int x;
                Child(int x) { this.x = x; }
                @Override void overrideMe() {
                    System.out.println(x);
                }
            }
            new Child(42); // prints "0"
        }
    }
    

    Here, when Base constructor calls overrideMe, Child has not finished initializing the final int x, and the method gets the wrong value. This will almost certainly lead to bugs and errors.

    Related questions

    • Calling an Overridden Method from a Parent-Class Constructor
    • State of Derived class object when Base class constructor calls overridden method in Java

    See also

    • FindBugs – Uninitialized read of field method called from constructor of superclass
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have something like this: public abstract class AbstractDataObject { public abstract void
I have a model that looks something like this: public abstract class Parent {
So I have a class is something like this public class Order { //some
Let's say I have something like this: abstract class Parent { protected function foobar($data)
Having something like this this : public abstract class AAA { protected abstract virtual
I want something like this public abstract class abc { public abstract void test();
For example can i have something like this: public static void SAMENAME (sameparameter n)
Suppose I have something like this: class A { public B mem; public int
I have something like this in my Spring Application: public class Book{ public Book(){
I have a program that looks something like this: public partial class It {

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.