Results 1 to 9 of 9
Thread: Question on Polyphormism
- 09-30-2009, 08:23 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
Question on Polyphormism
Given:
3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Zebra extends Mammal {
8. String name = "stripes ";
9. String makeNoise() { return "bray"; }
10. }
11. public class ZooKeeper {
12. public static void main(String[] args) { new ZooKeeper().go(); }
13. void go() {
14. Mammal m = new Zebra();
15. System.out.println(m.name + m.makeNoise());
16. }
17. }
What is the result?
A. furry bray
B. stripes bray
C. furry generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime
Question: At first look - my answer seems to be (B). Is that right?
It is related to polyphormism -- line 14 can tell..
- 09-30-2009, 08:30 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The answer should be A, run it and confirm for us.
Basically instance variables are resolved using the compile time type while the run time type is used to resolve methods.
So since m is a Mammal at compile time, m.name resolves to "furry " while m.makeNoise() will call the Zebra version of the method.Last edited by r035198x; 09-30-2009 at 09:21 AM.
- 09-30-2009, 09:09 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
yes.
Its option (A).
I think i need to practise alot more! Quite weak on all these...
Thanks for your help friend :)
- 09-30-2009, 09:27 AM #4
Stupid question maybe, but why is the name defined in both classes, and not set via a constructor? Doing this prevents certain types of polymorphism.
For example, let's say we added a getter to Mammal that returns the name. Unless we also add the getter to the Zebra class, it won't work as expected. (Added getName method and used a Zebra to store the Zebra object)
Java Code:class Mammal { String name = "furry "; String makeNoise() { return "generic noise"; } public String getName() { return name; } } class Zebra extends Mammal { String name = "stripes "; String makeNoise() { return "bray"; } // @Override // public String getName() // { // return name; // } } public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Zebra m = new Zebra(); // outputs furry bray (if Zebra.getName doesn't exist) // outputs stripes bray (if Zebra.getName exists) System.out.println(m.getName() + m.makeNoise()); } }
However, if we create a single name variable in the Mammal class, and set its value when constructing the object, we get the expected result. (Only added constructors to original code)
Java Code:class Mammal { String name; public Mammal() { this("furry "); } public Mammal(String name) { this.name = name; } String makeNoise() { return "generic noise"; } public String getName() { return name; } } class Zebra extends Mammal { public Zebra() { super("stripes "); } String makeNoise() { return "bray"; } } public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new Zebra(); // outputs stripes bray (as expected) System.out.println(m.name + m.makeNoise()); } }CodesAway - codesaway.info
writing tools that make writing code a little easier
- 09-30-2009, 09:31 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
No question is stupid on Java exams! :D
- 09-30-2009, 01:29 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
"name" is in both to see if people taking the test understand how the compiler and JVM work with this stuff.
- 09-30-2009, 01:53 PM #7
Aren't the tests suppose to include the practical uses only? I mean, I did UIL in High school for computer science, and they asked some of the most bizarre questions pertaining to C++, many that I would call poor programming practices.
Last edited by CodesAway; 09-30-2009 at 02:07 PM.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 09-30-2009, 02:00 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-30-2009, 02:08 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
RMI question
By alvandrood in forum New To JavaReplies: 1Last Post: 09-14-2009, 12:36 PM -
ur help on my question
By jameela in forum New To JavaReplies: 4Last Post: 10-13-2008, 12:38 PM -
question on jsp
By munigantipraveen in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 06-30-2008, 02:47 PM -
Question
By ayoood in forum New To JavaReplies: 16Last Post: 05-21-2008, 02:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks