• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class extend outer and method problem  RSS feed

 
Mikhail Bozhnev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

I have a problem with inner class what extend outer and outer method when i use it to inner object of inner class.

Example:


and


After creation of inner class object - die1 and using method GetValue() I always get "0".
How i should use inner class constructor to get "1" in GetValue() method?

Or i should user static nested classes instead?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly if you add getValue() to the inner class, it does return 1. This seemed odd to me so I opened the debugger. Where I was surprised to see there are two different value variables. At first, I didn't understand how this is since the second isn't declared.

Then I realized what was going on. Your inner class extends the outer class. So it IS a different value object because it is in a different Dice object.

screenshot.png
[Thumbnail for screenshot.png]
 
Mikhail Bozhnev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, do you have any thoughts about how can i use inner object "value" with method from outer object?

If i starting to use static nested classes, everything work fine:




but can we use non-static classes for this?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Interestingly if you add getValue() to the inner class, it does return 1. This seemed odd to me so I opened the debugger. Where I was surprised to see there are two different value variables. At first, I didn't understand how this is since the second isn't declared.

Then I realized what was going on. Your inner class extends the outer class. So it IS a different value object because it is in a different Dice object.



Wow. That is incredibly subtle !! This question feels like a brain teaser.

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry: Yeah, I had to try it because "everything looked right at first glance"

Mikhail: This works to access the value. Oh and by the way - welcome to CodeRanch!
 
Mikhail Bozhnev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Henry: Yeah, I had to try it because "everything looked right at first glance"

Mikhail: This works to access the value. Oh and by the way - welcome to CodeRanch!


Hi Jeanne and thank you)

Thank you for your answer, but this is not what I'm asking for

I don't want to create a method for class DiceD4, only for outher class Dices, because in future it will be lots of classes like DiceD4 - DiceD6, DiceD8 and etc.
I want implement method "getValue" only for class Dices and extend all inner classes to use it with inner class parameters. I can do it with static nested classes, but can i use non-static ones?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikhail Bozhnev wrote:
Hi Jeanne and thank you)

Thank you for your answer, but this is not what I'm asking for

I don't want to create a method for class DiceD4, only for outher class Dices, because in future it will be lots of classes like DiceD4 - DiceD6, DiceD8 and etc.
I want implement method "getValue" only for class Dices and extend all inner classes to use it with inner class parameters. I can do it with static nested classes, but can i use non-static ones?


First, do you understand why there is an issue? It would help greatly for you to resolve it yourself if you understood.

Anyway, as already mentioned, but repeating just in case.... there are two value instance variables here and one is hiding the other -- and in your case, one method is using one, and the other method is using the other. So, you have a few options...

1. Get rid of one by doing what you did... making the subclasses as nested classes.
2. Get rid of one by making the subclasses regular classes -- instead of inner classes. BTW, why do they have to be inner classes in the first place?
3. Get rid of one by getting rid of the inheritance relationship -- which means that you can't use a common class to roll a diceD4 and a diceD20.

4. Or make the methods use the same one... like so...



Of the options, I like option two the best. There is no reason for the subclasses to be inner classes.


And I really don't like option 4 (even though I gave the code for it). The specification doesn't seem that clear on how to prevent one mechanism from hiding the value from the other mechanism -- so, depending on how you use it, this work-around may break.

Henry
 
Mikhail Bozhnev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

Of the options, I like option two the best. There is no reason for the subclasses to be inner classes.


And I really don't like option 4 (even though I gave the code for it). The specification doesn't seem that clear on how to prevent one mechanism from hiding the value from the other mechanism -- so, depending on how you use it, this work-around may break.

Henry


Hi Henry, thanks for the complete answer!

I choose option number one and make nested classes and define methods for each nested class.



I can't find any other way to keep all "DiceD#" in one file. I'm really don't want to create separated class file for each "DiceD#".
And I can't understand how implement method "getValue" only in "Dices" class with "values" from inner classes without create "value" in "Dices" class... ((
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikhail Bozhnev wrote:
I can't find any other way to keep all "DiceD#" in one file. I'm really don't want to create separated class file for each "DiceD#".
And I can't understand how implement method "getValue" only in "Dices" class with "values" from inner classes without create "value" in "Dices" class... ((


First... why one file? You can't maintain more than one file?

Second... inner classes (or nested classes) creates a separate class file. Take a look, you will see multiple class files being generated.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!