aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ZooKeeper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ZooKeeper" Watch "ZooKeeper" New topic
Author

ZooKeeper

Bert Porter
Greenhorn

Joined: Feb 10, 2009
Posts: 12
I'm having trouble with question 13 in the Self Test for Ch. 2 of K&B SCJP6.
Given:

What is the result?
The result is "furry bray" but I don't see why it wouldn't be "stripes bray". m refers to a Zebra. I tried the following to sort it out, but it hasn't exposed the key to me.

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

the answer is furry bray, correct. This is because instance variables are not overridden ,never. As the reference variable is of type Mammal the value of m.name is decided at the compile time and as the object to which it is pointing is of subtype(which is legal) i.e Mammal m = new Zebra() the method that is called is decided at run-time (in case of overridden methods--dynamic binding) depending on the object the reference variable is pointing to. Hence the answer furry bray.

Hope this explains...


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Bert Porter
Greenhorn

Joined: Feb 10, 2009
Posts: 12
m and z2 both refer to the same object. But m.name is different from z2.name. I thought that m was just a type (Mammal) and a pointer to an object. So m.name would mean, go to the object which m refers to, and retrieve the value of name from that object. Apparently, I am wrong. Somehow, m maintains its own value of the instance variable 'name', independent of the object it refers to. I think that's kooky.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Mammal m = new Zebra();


Remember, Superclass reference variable(In this caseL m) dont refer to instance variables of subclass. As Sudipto pointed, you cannot ovveride instance variable.
But, Superclass reference variable can refer to subclass methods using dynamic binding as above.

So why instance variables cannot be overriden OR why only just reference variable should be overriden.

Think for some time..you will get the answers.


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Bert Porter
Greenhorn

Joined: Feb 10, 2009
Posts: 12
Himalay says, "Remember, Superclass reference variable(In this caseL m) dont refer to instance variables of subclass." I don't get this. Doesn't a reference variable refer to an object, i.e. an instance? Doesn't an instance variable belong to an instance? It isn't a static variable. So, m refers to an object on the heap. The dot operator goes to that object, right? The object has an instance variable called 'name'. The object (not the class) has a value for that instance variable.
Bert Porter
Greenhorn

Joined: Feb 10, 2009
Posts: 12
I think a Zebra object must actually have two instance variables called 'name' - and which one is visible depends on whether your reference variable thinks it's looking at a Zebra or a Mammal. I'd call this a pathogenic subclass. In fact, I'd advocate making it illegal to do this.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
m.name is resolved at compile time by compiler, compiler see only reference type to call name, here reference type is Mammal, so it will call Mammal's name.

m.makeNoise() is resolved at runtime by the JVM, JVM see actual object instance pointed by reference, so it will get zebra object and it will call zebra's makeNoise().

Polymorphism applies to instance methods only.


SCJP 6
Bert Porter
Greenhorn

Joined: Feb 10, 2009
Posts: 12
OK, I read on into chapter 3, and when I got to shadowing, I saw that that's what we have here.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
When you say String name="mammal";

It means a reference variable name is pointing to the newly created String object "mammal".
In the subclass using shadowing(not overriding), you are assigning the same reference variable name to a new object "zebra"

The method names in line3 and line23 differ, so no method overriding happening.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ZooKeeper
 
Similar Threads
Question in scjp study guide (Kathey & Bates) page 181- oo Concepts
I need to help with the question 13 chapter 2
output of program?
Upcasting doesn't work as intended; clarification needed
instance variable inheritance