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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:25:23+00:00 2026-06-06T07:25:23+00:00

Have a look at following scenario: public class ParentClass { private Integer testVar =

  • 0

Have a look at following scenario:

public class ParentClass {

    private Integer testVar = 1;

    public Integer getTestVar() {
        return testVar;
    }

    public void setTestVar(Integer testVar) {
        this.testVar = testVar;
    }
}


public class ChildClass extends ParentClass {

    private Integer testVar = 2;

    @Override
    public Integer getTestVar() {
        return testVar;
    }

    @Override
    public void setTestVar(Integer testVar) {
        this.testVar = testVar;
    }
}


public class TestClass {

    public static void main(String[] args) {

        ChildClass childClass = new ChildClass();
        childClass.setTestVar(3);
        Gson gson = new Gson();
        String str = gson.toJson(childClass);
        System.out.println(str);
    }
}

Here, I have used com.google.gson.Gson package for json conversion. It gives following output:

{“testVar”: 1}

I expected 3 in the json string, but what I got was the value held by testVar variable of parentClass.

  1. Shouldn’t child’s getTestVar over-ride the parent’s counterpart?

  2. Is instance member being accessed directly (changing name of instance members of each class, but keeping getters name same proves this)?

  3. If yes, how can anyone access private members directly?

EDIT: I’ve edited this question. This version only shows parent’s value in json. Is there a way to get child’s instance member?

  • 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-06T07:25:24+00:00Added an answer on June 6, 2026 at 7:25 am
    1. Yes, it does

    2. Yes, Gson accesses fields directly. Other libraries that perform similar tasks often allow you to choose between field and property (i.e. using getters and setters) access, but Gson only supports field access.

    3. Private fields are intended to be inaccessible for the regular code, but in some special cases (such as serialization/deserialization) it’s perfectly reasonable to access them directly. That’s why you can access private fields using reflection (see AccessibleObject).

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

Sidebar

Related Questions

please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Lets say i have the following scenario: public class A { public String createString(final
Have a look at following code: @Local public interface MyService { void printMessage(); }
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
Lets say i have the following set of interfaces.... public interface IStart { void
Have a look at the following Scala example: class A { def foo(x: Int):
I have the following scenario... When I hover over span.share-this , it triggers a
I have a following scenario. A list of table rows. ... <tr><td><a href=# class=item

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.