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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:05:46+00:00 2026-06-10T17:05:46+00:00

In Java, how do I override the class type of a variable in an

  • 0

In Java, how do I override the class type of a variable in an inherited class? For example:

class Parent {
  protected Object results;

  public Object getResults() { ... } 
}

class Child extends parent {

  public void operation() { 
  ... need to work on results as a HashMap
  ... results.put(resultKey, resultValue);
  ... I know it is possible to cast to HashMap everytime, but is there a better way?
  }

  public HashMap getResults() {
  return results;
}
  • 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-10T17:05:48+00:00Added an answer on June 10, 2026 at 5:05 pm

    You could use generics to achieve this:

    class Parent<T> {
        protected T results;
    
        public T getResults() {
            return results;
        } 
    }
    
    class Child extends Parent<HashMap<String, Integer>> {
    
        public void operation() { 
            HashMap<String, Integer> map = getResults();
            ...
        }
    }
    

    Here I used key and value types of String and Integer as examples. You could also make Child generic on the key and value types if they vary:

    class Child<K, V> extends Parent<HashMap<K, V>> { ... }
    

    If you’re wondering how to initialize the results field, that could take place in the constructor for example:

    class Parent<T> {
    
        protected T results;
    
        Parent(T results) {
            this.results = results;
        }
    
        ...
    }
    
    class Child<K, V> extends Parent<HashMap<K, V>> {
    
        Child() {
            super(new HashMap<K, V>());
        }
    
        ...
    }
    

    Some side notes:

    It would be better for encapsulation if you made the results field private, especially since it has the accessor getResults() anyway. Also, consider making it final if it’s not going to be reassigned.

    Also, I’d recommend programming to interface by using the Map type in your public declarations rather than HashMap specifically. Only reference the implementation type (HashMap in this case) when it’s instantiated:

    class Child<K, V> extends Parent<Map<K, V>> {
    
        Child() {
            super(new HashMap<K, V>());
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class CursorAtStartFocusListener extends FocusAdapter { @Override public void focusGained(java.awt.event.FocusEvent evt) { Object source
GIN Module: public class InjectorModule extends AbstractGinModule { @Override protected void configure() { bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
How come java doesn't allow the following generic return type: public <T extends Enum<T>
I have the following Java class public class MyClass <T extends MyInterface> implements RowMapper<MyInterface>
Possible Duplicate: Java: Always override equals? should I override equals function for any class
It is possible in plain Java to override a method of a class programmatically
I tried scala> class Foo extends Component { var font = new java.awt.Font(Helvetica, java.awt.Font.BOLD,
I've got problem in my code in Java. I have four(important) Classes: public class
I have the following two classes: import java.io.*; import java.util.*; public class User {
I know that Java will let you do trickery with a variable's type when

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.