This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Issue with Rule Round-up Game Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Issue with Rule Round-up Game " Watch "Issue with Rule Round-up Game " New topic
Author

Issue with Rule Round-up Game

Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

I didn't get the logic behind the output of this code i found in Rule Round-up Game, Question #159. The output is correct,but i've a doubt.

Though class B inherits method(),why it shouldn't access its own member variable x, rather than priting 10, why it prints 9.Can anyone explain.


Thanks & Regards
Sidharth Pallai
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Because the object you're calling the method on is type A.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Bill Cruise:
Because the object you're calling the method on is type A.


Correct.

But even

new B().method();

would print 5. And *that* is because *fields* are *not* polymorphic, only methods are.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

Thank Preuss,
But i didn't get idea of how fields being non-polymorphic.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

The variable A.x and the variable B.x are different variables. Even though they have the same name, they're not related to each other, and a B object contains one of each. The code in method() knows that, when it is compiled, "x" means "A.x", and therefore, no matter how that method is called, it's the x in class A that will be used.

Compare your code to this version of class B:



This version prints "10", for two reasons. First, because we're using an instance of class B, not A. But as Ilja said, that wouldn't be enough with the original code, which still prints "5" even if you create an instance of B. In this version, instead of creating a new, separate variable, we just assign a new value to the existing one; and then method() sees that new version.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Issue with Rule Round-up Game
 
Similar Threads
Inheritance
Polymorphism
Question on scope
overriding conecpt(please help me)
Methods are overridden, variables are shadowed