Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Question

 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when a class inherits a method, would it be right to think of it as a copy of the method is placed in the inheriting class ?

That's how I used to think of it, but after experimenting w/ the code below, I see it more that the parent classes are created as well.

The code below outputs 'one'. While I would expect 'two'. I would expect the "return (this) name" line to point to the 'Over' class when referenced from the Over reference.



 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables aren't polymorphic; a variable in a subclass in no sense "overrides" a variable of the same name in the superclass. An object of subclass type includes both variables somewhere in its guts.

No copy of the code is made; the code remains attached to the superclass, and is invoked with the subclass object as the "this" reference. But as the superclass method is compiled to look at the superclass variable "n", it will always look at that variable -- not another variable by the same name in the subclass.
 
BV Boose
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a subclass inherits all the members of the superclass, including fields. If you override the getName() method in Over, you'll get the Over.name instead of Under.name
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Overriding vs Hiding. It applies to fields too.
[ December 16, 2008: Message edited by: Rob Prime ]
 
BV Boose
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but that doesn't apply in this case. It's not a subclass declared as a super class, it's just straight up overriding.

vs


from that site- will this even compile? Foo is the super class,


shouldn't that be:



Originally posted by Rob Prime:
Read Overriding vs Hiding. It applies to fields too.

[ December 16, 2008: Message edited by: Rob Prime ]

[ December 16, 2008: Message edited by: BV Boose ]
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from that site- will this even compile? Foo is the super class,

... Code deleted ...

shouldn't that be:



If "Foo is the super class" of Bar, then Bar objects IS-A Foo, hence, you can assign a Bar instance to a Foo reference. The example is correct.

Your code is not. A Foo object is not a Bar object.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but that doesn't apply in this case. It's not a subclass declared as a super class, it's just straight up overriding.



Regardless, no overridding is taking place. There is only one getName() method, and the subclass doesn't override it. There is no overriding here -- straight up or otherwise.

Henry
 
BV Boose
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll concede the object declaration- I always mix that up, so I tried to compile the example and it wouldn't work for me.
Not seeing how you figure it's hiding. getName() is not a static method, why is that hidden and not overridden?
And thanks for the snark both times.
Originally posted by Henry Wong:



Regardless, no overridding is taking place. There is only one getName() method, and the subclass doesn't override it. There is no overriding here -- straight up or otherwise.

Henry

[ December 16, 2008: Message edited by: BV Boose ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by BV Boose:
I'll concede the object declaration- I always mix that up, so I tried to compile the example and it wouldn't work for me.
Not seeing how you figure it's hiding. getName() is not a static method, why is that hidden and not overridden?
And thanks for the snark both times.


The OPs questions are not about overriding methods -- they're about an attempt to override the variable "n". The child's "n" hides the parent's "n" -- it doesn't override it. Because of this, calling getName() returns the value of the parent's "n", not the child's.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not seeing how you figure it's hiding. getName() is not a static method, why is that hidden and not overridden?
And thanks for the snark both times.


With the getName() method, it is neither hiding or overriding. The subclass doesn't have a getName() method.

On the subject of hiding, EFH and Rob are referring to the "name" field.

Henry
 
BV Boose
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotcha, I'm comparing apples and oranges. Over.name hides Under.name, the getName() I added to the subclass overrides super.getName().
But why doesn't Over.name hide Under.name even when super.getName is called?
reference:

Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super, which is covered in the next section. Generally speaking, we don't recommend hiding fields as it makes code difficult to read.


from http://java.sun.com/docs/books/tutorial/java/IandI/hidevariables.html
Originally posted by Ernest Friedman-Hill:


The OPs questions are not about overriding methods -- they're about an attempt to override the variable "n". The child's "n" hides the parent's "n" -- it doesn't override it. Because of this, calling getName() returns the value of the parent's "n", not the child's.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by BV Boose:

But why doesn't Over.name hide Under.name even when super.getName is called?


Because the code for getName() is not within the class Over; it's within the class Under. The code for Under.getName() is hard-coded to use the variable Under.name. Once the class Under is compiled, nothing can make that code act any differently.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic