Results 1 to 9 of 9
  1. #1
    DrMath is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Question 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..

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  3. #3
    DrMath is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    yes.

    Its option (A).
    I think i need to practise alot more! Quite weak on all these...
    Thanks for your help friend :)

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    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

  5. #5
    DrMath is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    No question is stupid on Java exams! :D

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    "name" is in both to see if people taking the test understand how the compiler and JVM work with this stuff.

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    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

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by CodesAway View Post
    Aren't the testes suppose to include the practical uses only? ..
    No .

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    Exactly.
    They're testing technical knowledge, which includes understanding what is actually going on with polymorphism in Java. Knowing the answer to the above question means you are less likely to get caught out.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. RMI question
    By alvandrood in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 12:36 PM
  3. ur help on my question
    By jameela in forum New To Java
    Replies: 4
    Last Post: 10-13-2008, 12:38 PM
  4. question on jsp
    By munigantipraveen in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-30-2008, 02:47 PM
  5. Question
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 05-21-2008, 02:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •