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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:02:09+00:00 2026-05-14T05:02:09+00:00

I know abstract fields do not exist in java. I also read this question

  • 0

I know abstract fields do not exist in java. I also read this question but the solutions proposed won’t solve my problem. Maybe there is no solution, but it’s worth asking 🙂

Problem

I have an abstract class that does an operation in the constructor depending on the value of one of its fields.
The problem is that the value of this field will change depending on the subclass.
How can I do so that the operation is done on the value of the field redefined by the subclass ?

If I just “override” the field in the subclass the operation is done on the value of the field in the abstract class.

I’m open to any solution that would ensure that the operation will be done during the instantiation of the subclass (ie putting the operation in a method called by each subclass in the constructor is not a valid solution, because someone might extend the abstract class and forget to call the method).

Also, I don’t want to give the value of the field as an argument of the constructor.

Is there any solution to do that, or should I just change my design ?


Edit:

My subclasses are actually some tools used by my main program, so the constructor has to be public and take exactly the arguments with which they will be called:

tools[0]=new Hand(this);
tools[1]=new Pencil(this);
tools[2]=new AddObject(this);

(the subclasses are Hand, Pencil and AddObject that all extend the abstract class Tool)

That’s why I don’t want to change the constructor.

The solution I’m about to use is to slightly change the above code to:

tools[0]=new Hand(this);
tools[0].init();
tools[1]=new Pencil(this);
tools[1].init();
tools[2]=new AddObject(this);
tools[2].init();

and use an abstract getter to acces the field.

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

    How about abstract getter/setter for field?

    abstract class AbstractSuper {
        public AbstractSuper() {
            if (getFldName().equals("abc")) {
                //....
            }
        }
    
        abstract public void setFldName();
        abstract public String getFldName();
    }
    
    class Sub extends AbstractSuper {
        @Override
        public void setFldName() {
            ///....
        }
    
        @Override
        public String getFldName() {
            return "def";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I commented earlier on this question (Why java.lang.Object is not abstract?) stating that I'd
I know that this sort of question has been asked here before, but still
i know this is a stupid question but i d'ont know how to do
I'm learning Java and as I know only abstract classes and interfaces cannot be
Know this might be rather basic, but I been trying to figure out how
I know you can not set a key value dynamically, but what about the
I know this should be a pretty elementary issue to fix, but 1) I'm
I know that Phonegap has an event for back button, but it's only available
If I have an abstract class in java named Foo and it has an
I have a quite simple question: I want to have a Java Class, which

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.