• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism Doesn't Apply to Statics

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, as I have written the subject line, I can clearly understand that Polymorphism doesn't apply to Statics. If I am correct, polymorphism gives multiple options for referencing an instance of a class. Static pertains to the class itself, regardless of instance.

Still, I would like clarification on two code snippits at the Q&A section of Chapter 2 for the SCJP 6 Book I am studying from:

SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)
By: Kathy Sierra; Bert Bates
Print ISBN-10: 0-071-59106-0

I have attached screenshots of two questions relating to Polymorphism and Statics:

Chapter_2_Question_10.png where the answer is fa la
Chapter_2_Question_13.png where the answer is furry bray

Here is my problem, how do I even trace this code to get the output?

Thanks in advance
Chapter_2_Question_13.png
[Thumbnail for Chapter_2_Question_13.png]
Chapter_2_Question_10.png
[Thumbnail for Chapter_2_Question_10.png]
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always keep in mind two things
  • Static methods and varialbles are always related to the class only and they have nothing to do with the objects
  • And polymorphism applies only to the methods NOT the variables

  • Now in the first case we are declaring as follows

    here both the sing methods are static and hence they will be called due to the class reference and not the object reference
    hence
    t.sing() outputs "fa"
    s.sing() outputs "la"

    and in the second example
    always remember that polymorphism applies only to method not the variables
    hence
    even if we do as follows
    the object is of zebra and hence the overriding method makeNoise () gets called as POLYMORPHISM APPLIES TO METHODS
    and when we call m.name, " furry" gets printed because Polymorphism does not apply to variables
     
    Sandra Bachan
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the clarification. It seems Chapter 2 didn't adequately explain this, or perhaps it will come up later in the text.
     
    Ranch Hand
    Posts: 430
    Android VI Editor Debian
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I had the same problem with question 15.
     
    You showed up just in time for the waffles! And this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic