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

  • Home
  • SEARCH
  • 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 6227043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:08:38+00:00 2026-05-24T09:08:38+00:00

While exploring for scjp questions, I came across this behaviour which I found strange.

  • 0

While exploring for scjp questions, I came across this behaviour which I found strange.

I have declared two classes Item and Bolt as follows:

class Item {
    int cost = 20;

    public int getCost() {
        return cost;
    }
}

class Bolt extends Item {
    int cost = 10;

    public int getCost() {
        return cost;
    }
}

and tried to access the value of cost twice

public class Test {
    public static void main(String[] args) {
        Item obj = new Bolt();
        System.out.println(obj.cost);
        System.out.println(obj.getCost());
    }
}

The output I get is 20 10.
I can’t understand how this happens.

  • 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-24T09:08:40+00:00Added an answer on May 24, 2026 at 9:08 am

    obj is a reference of type Item hence the first 20 since the value of cost field of Item is 20. The second value is 10 because the runtime type of obj is Bolt and hence getCost() invokes getCost of Bolt class (since Bolt extends Item).

    In short, runtime polymorphism is applicable to only instance members (method overriding) and not instance fields.

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

Sidebar

Related Questions

while exploring jQuery I came up with the following weird script. I don't see
While recently exploring prezi I realized there have to be better tools than PowerPoint
I've been exploring WPF and XAML for a while now, but have hit a
I have been developing web apps for a while now and for the past
I'm trying to solve a two-dimensional random walk problem from the book, exploring python.
I have been exploring OAuth version 1.0 for the REST API I am currently
*I'm using Java. I have this thread, agent, that explores a room to determine
I am a newbie just started exploring Grails so that I can recommend this
While exploring msdn sites ,most of the condition checking places they are using (NULL
I have a large data set of nucleotide sequences (long strings simply put) which

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.