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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:55:22+00:00 2026-05-25T11:55:22+00:00

I ran into this example about inheritances on the web, and I am not

  • 0

I ran into this example about inheritances on the web, and I am not easy regarding its results. I’m missing something critical.

public class A {

int i = 5;

public A() {
    foo();
}

private void foo() {
    System.out.println(i);

}}

public class B extends A {
int i = 6;}

public class C extends B {
int i = 7;

public void foo() {
    System.out.println("C's foo");
    System.out.println(super.i);
}}

I’m trying to figure out what’s happening by the following command: C c = new C();
System.out.println(C.i);

I know that when we create a new instance of C we approach A’s and B’s constructures, So We reach to A() – (question 1) Does i (of A) being initialize on the way?
Now we need to call to foo()– (question 2)- Does C’s foo() consider as an override of A’s foo()? and what if B had a foo() of itself? then it was consider as a override and C’s foo() was operated?

As far as I know, there is no override when it relates to local variables. How come that System.out.println(c.i) is 7 and not 5? shouldn’t it b the i of the highest-father?

EDIT: My question is not about which foo and i will be used when I use c, is about what happens during these two specific commands, cause obviously A’s foo was used and not C’s.

Thank you very much.

  • 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-25T11:55:23+00:00Added an answer on May 25, 2026 at 11:55 am

    The three i variables here are completely independent. Which one is used by any statement is determined at compile-time – there’s no polymorphism involved. So A.foo() will always print out the value of the variable declared in A.

    Note that these aren’t local variables by the way – they’re instance variables.

    When you print out c.i that uses the variable declared in C because the compile-time type of c is C. You can see this if you write:

    C c = new C();
    A a = c;
    B b = c;
    System.out.println(a.i); // 5 - variable declared in A
    System.out.println(b.i); // 6 - variable declared in B
    System.out.println(c.i); // 7 - variable declared in C
    

    Note that in a well-written program this sort of thing almost never causes a problem, as variables should be private.

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

Sidebar

Related Questions

I ran into this dilemma when working on an ASP.net web application using Web
Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's
This is a question I ran into about expanding on an element's JavaScript onchange
Just about everyone has ran into this specific issue: function addLinks () { for
Quick question about something I've ran into alot recently: So many times I'm looking
I am learning about the MySQL statement, ORDER BY . I ran into this
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
I ran into this error when building my code with CLANG: In file included
Never ran into this problem with jQuery before. I have the following: $(document).ready(function() {
I ran into this situation today. I have an object which I'm testing for

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.