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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:38:01+00:00 2026-06-10T05:38:01+00:00

a child class extending a mother class which, in its constructor call a template

  • 0

a child class extending a mother class which, in its constructor call a template method instianted in the child class because it need a value obtained in the child constructor.

How Can I do something like this, without changing my Parent constructor (Foo, here. I cannot do any changes in the real Foo class):

public abstract class Foo{
    public Foo(){
        register();
    }

    public abstract void register();

    public void aMethod(int aValue){
        // body
    }

}

public class Bar extends Foo{

    public Foo(int aValue){
        // body
    }

    register(){
        aMethod(aValue);
    }
}

Here, even if I put aValue in a field, aValue is not even created at

aMethod(aValue);.

How can I solve my problem?

I am looking for any pattern, ULM, solution.

  • 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-10T05:38:02+00:00Added an answer on June 10, 2026 at 5:38 am

    a child class extending a mother class which, in its constructor call a template method instianted in the child class because it need a value obtained in the child constructor.

    Why doesn’t the subclass constructor simply pass the value up to the superclass constructor?

    public abstract class Foo {
        protected Foo(int value) {
           ... use value ...
       }
    }
    
    public class Bar extends Foo {
        public Foo(int value) {
            super(value);
        }
        ...
    }
    

    Note that calling virtual methods in a constructor is a dangerous practice, as the subclass won’t have had the opportunity to initialize itself yet.

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

Sidebar

Related Questions

When a child class overrides multiple methods and calls a method in its parent
Why is the Child class's converter constructor called in the code below? I mean,
I have a parent and child class that both need to implement IDisposable .
I have created a class extending SimpleXMLElement which loads an XML containing translations for
I have a parent class and a child class which inherits parent. In a
Im having problems extending a class which is defined in a gem and is
I am extending a class defined in a library which I cannot change: public
I would like to make a child class that has a method of the
I have a parent class and its child in my application model. And have
Why Child class doesn't have echo() method? Parent = function(){ this.name = 'abc'; }

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.