• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

From Sierra K., Bates B. - OCA OCP Java SE 7 Programmer I & II Page 161 - SelfTest Explanation

 
Zoran Velickovski
Greenhorn
Posts: 12
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Sierra K., Bates B. - OCA OCP Java SE 7 Programmer I & II
Page 161 - SelfTest

In this case ... why makeNoise() is call from Zebra and name is from Mammal
Tnx





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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zoran Velickovski,

First of all, a warm welcome to CodeRanch!

Zoran Velickovski wrote:In this case ... why makeNoise() is call from Zebra and name is from Mammal

To answer this question correctly, you need to know these 2 very simple (and hopefully easy to remember) but very, very, very important rules:
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • In this topic you'll find a very similar example explained in great detail. And here you'll find another topic about a similar example with excellent explanations (and several code snippets). Both threads are definitely worth reading. So I would advice to read these threads and if you still have questions or doubts, just hit the reply button and let us know!

    Hope it helps!
    Kind regards,
    Roel

    PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. Jeanne already added the code tags for you. See how much easier the code is to read?
     
    Zoran Velickovski
    Greenhorn
    Posts: 12
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry but I don't have a clear blue sky after this explanation (with links)

    If you have time ... please explain me on this example

    It will be good from you and my doubt will be from zero to hero


    TNX
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Zoran Velickovski wrote:If you have time ... please explain me on this example

    Do you understand the two very simple rules I mentioned in my 1st post? Because those rules clearly explain the output for this example. And in my explanation I would apply those rules to your code snippet. But if you don't understand those rules, that would not be very helpful.

    Before you can answer this question correctly, you should have a good, solid understanding of polymorphism.
     
    Zoran Velickovski
    Greenhorn
    Posts: 12
    2
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fields in dynamic polimorfism is taken from SuperClass and methods are taken from SubClass

    If I put

    in class Mammal

    and

    in class Zebra

    then
    print (fields m.name and m.vtor) who are from SuperClass - Mammal

    Correct me if I'm wrong


     
    Zoran Velickovski
    Greenhorn
    Posts: 12
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This part of explanation (from this post) tell me all

    Roel De Nijs wrote:this is the flow of execution:
    1/ b.h is evaluated -> 4 (rule 1: which instance variable is accessed, is determined at compile time based on the type of the reference variable -> b is of type Baap)
    2/ 4 and " " are concatenated -> "4 "
    3/ b.getH() is evaluated -> getH() method in Question37 class is executed (rule 2: which method is executed, is determined at runtime based on the type of the actual object -> actual object is of type Question37)
    3a/ the statement System.out.println("beta "+h); is executed
    3a1/ h is evaluated -> 44
    3a2/ "beta " and 44 are concatenated -> "beta 44"
    3a3/ "beta 44" is printed
    3b/ h is evaluated -> 44
    3c/ 44 is returned
    4/ "4 " and 44 are concatenated -> "4 44"
    5/ "4 44" is printed
    And that's why "beta 44" is in the output before "4 44".



    TNX a lot Roel De Nijs - yrthebest
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Zoran Velickovski wrote:print (fields m.name and m.vtor) who are from SuperClass - Mammal

    Correct me if I'm wrong

    You are absolutely spot-on! You have successfully applied the first (very, very, very important) rule ("Which instance variables you can access is determined at compile time based on the reference variable type.") to your code snippet. Have a cow!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic