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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:44:06+00:00 2026-06-12T03:44:06+00:00

My head hurts from thinking about an elegant solution to the following problem. I

  • 0

My head hurts from thinking about an elegant solution to the following problem. I have a parent class and several subclasses. My parent class declares and implements a method to retrieve properties, but the name of the property file is determined by the subclasses.

I’m only working with instances of the subclasses.

The method (implemented in the parent) should use the field values of the subclass, but since I’m using it in the parent class, I need to declare it there as well.

Now, every time I call the method from a subclass the field value of the parent class is used.

Some simple code to illustrate my problem (please keep in mind: it’s not about functionality – more about the design). The subclass defining the field value I want to use:

public class SubClass extends Parent {
  public static final String CONFIG_FILE = "subclass.properties"; // same problem with non-static
  public SubClass() {
    System.out.println(getProperty("somekey"));
  }
}

and the parent class defining and implementing the method I want to use:

public class Parent {
  public static final String CONFIG_FILE = "config.properties"; // same problem with non-static
  public String getProperty(String key) {
    Properties props = new Properties();
    try {
      props.load(new FileReader(new File(CONFIG_FILE)));
    }
    catch (IOException ioe) {
      return null;
    }
    return props.get(key);
  }  

Since I’m calling getProperty(..) from the subclass, I would expect it to use the field value of the instance it is operating on (“subclass.properties”), but instead it’s using the parent field value (“config.properties”). I got the same effect with non-static fields.

  • 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-12T03:44:07+00:00Added an answer on June 12, 2026 at 3:44 am

    Put this in your subclass. This will then return the properties for the subclass. This method would override the one in the parent class.

    public String getProperty(String key) {
       return CONFIG_FILE;
     }
    

    Incidently you are shadowing the variable.

    Alternatively I would recommend passing in the variable name to the constructor and then have it call super.

    public SubClass() {
        super(SUB_CLASS_CONFIG);
    }
    

    and then have the Parent construtor something like

    public Parent(String key) { 
         CONFIG_FILE = key;
    }
    

    then you will not have to override the method. The method can just return config in the parent. I would think carefully as to whether you want it to be static though….

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

Sidebar

Related Questions

i have tried till my head hurts and have been reading so much my
My head is about to explode with this logic, can anyone help? Class A
Head First book says about include directive as include code from any other file
OK, my head hurts...! This beautiful MySQL query: (SELECT mtwitterfollowers AS twitfollow FROM `media`
I have this sample text, which is retrieved from the class name on an
My head hurts: I've read so many blogs about C++11x's move semantics that my
Hi I have seemingly simple problem which is causing my head to hurt. I
My head is about to explode with everything I've read about SQL Server and
My current head scratcher: implementing a model class specifically for service calls to my
my head hurts today! :) I need some help with rails deployment. I migrated

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.