File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inheritance Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance Question" Watch "Inheritance Question" New topic
Author

Inheritance Question

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

Read Overriding vs Hiding. It applies to fields too.
[ December 16, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Jul 26, 2008
Posts: 33
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Jul 26, 2008
Posts: 33
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Question