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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:19:42+00:00 2026-05-15T06:19:42+00:00

I am just refreshing the oops features of the java. So, I have a

  • 0

I am just refreshing the oops features of the java. So, I have a little confusion regarding inheritance concept. For that I have a following sample code :

class Super{
    int index = 5;
    public void printVal(){
        System.out.println("Super");
    }
}
class Sub extends Super{
    int index = 2;
    public void printVal(){
        System.out.println("Sub");
    }
}
public class Runner {
    public static void main(String args[]){
        Super sup = new Sub();
        System.out.println(sup.index+",");
        sup.printVal();
    }
}

Now above code is giving me output as : 5,Sub.

Here, we are overriding printVal() method, so that is understandable that it is accessing child class method only.

But I could not understand why it’s accessing the value of x from Super class…

Thanks in advance….

  • 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-15T06:19:43+00:00Added an answer on May 15, 2026 at 6:19 am

    Objects have types, and variables have types. Because you put:

    Super sup = new Sub();
    

    Now you have a variable sup of type Super which refers to an object of type Sub.

    When you call a method on an object, the method that runs is chosen based on the type of the object, which is why it prints “Sub” instead of “Super”.

    When you access a field in an object, the field is chosen based on the type of the variable, which is why you get 5.

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

Sidebar

Related Questions

I have the following code: private void button1_Click(object sender, EventArgs e) { var answer
I was just refreshing my JavaScript/Jquery skills and I thought of making a basic
Just checking my JS and I have an error, but I cannot see where.
Just a knowledge question which I would like to ask: For example, I have
just wondering if there is a way to reduce the amount of code needed
I have a Highchart's graph that needs to be regenerated every minute. I was
I have a chat that uses textarea instead of text for obvious reasons. This
I have a semi-complicated problem and hoping that someone here will be able to
I'm just trying to get into NAnt for my build processes, and successfully have
I have been working on a Ruby Script that enters in data into a

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.