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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:52:14+00:00 2026-06-16T14:52:14+00:00

I searched online for similar question, but could not find it. So, posting here.

  • 0

I searched online for similar question, but could not find it. So, posting here.

In the following program why the value of ‘i’ is printed as 100?

AFAIK ‘this’ refers to the current object; which in this case is ‘TestChild’ and the class name is also correctly printed. But why the value of the instance variable is not 200?

public class TestParentChild {
    public static void main(String[] args) {
        new TestChild().printName();
    }
}

class TestChild extends TestParent{
    public int i = 200;
}

class TestParent{
    public int i = 100;
    public void printName(){
        System.err.println(this.getClass().getName());
        System.err.println(this.i); //Shouldn't this print 200
    }
}

And moreover the output of the following is as i expected. The child class method is invoked, when i call “this.test()” from Parent class.

public class TestParentChild {
    public static void main(String[] args) {
        new TestChild().printName();
    }
}

class TestChild extends TestParent{
    public int i = 200;
    public void test(){
        System.err.println("Child Class : "+i);
    }

}

class TestParent{
    public int i = 100;
    public void printName(){
        System.err.println(this.getClass().getName());
        System.err.println(this.i); //Shouldn't this print 200
        this.test();
    }
    public void test(){
        System.err.println("Parent Class : "+i);
    }
}
  • 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-16T14:52:15+00:00Added an answer on June 16, 2026 at 2:52 pm

    Java does not have virtual fields, so the i field in printName always refers to TestParent.i and not any descendant child.

    Polymorphism through inheritance in Java only happens with methods, so if you want the behaviour you’re describing then you’d want this:

    class TestChild extends TestParent{
    
        private int i = 200;
    
        @Override
        public int getI() { return this.i; }
    }
    
    class TestParent{
    
        private int i = 100;
    
        public int getI() { return this.i; }
    
        public void printName(){
            System.err.println( this.getClass().getName() );
            System.err.println( this.getI() ); // this will print 200
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have already searched everywhere but could not find a solution for the problem.
I've searched online and on here but can't find anything which seems to fit
I have searched online for a solution, but have yet to find one that
I realize this is a basic question but I have searched online, been to
I've searched around online but can't find what I'm after. Basically, during an install,
Here we go again , searched and tried myself but none of the online
I've searched online and on stackoverflow but couldn't find one good answer how to
I've searched online and on this site and I can not find a good
I've searched for similar questions but didn't find any. The usual problem is that
I searched online for a C++ Longest Common Substring implementation but failed to find

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.