This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Static versus non-static

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given below is a subclass SmartPhone that extends a parent class Phone:




Question: Is the reason behind the output

Call-Phone
Call-Phone

for the static methods 'call' in (parent and subclass) the following:

Each method call (lines 3 and 4) is statically bound to the class that is used to declare reference variable.

On lines 1 and 2, the base class "Phone" is being used to declare each reference variable 'phone' (line 1) and 'smartPhone' (line 2).

Because the methods are static, polymorphism does not come into play,
and each static method called (lines 3 and 4) is bound to the class (in this case the base class) used to declare the reference variable.

Please verify.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, yes.

To avoid the potential confusion, you should avoid doing this - calling a static method on an instance - and instead always call Phone.call() or SmartPhone.call() explicitly. But if you do call it on an instance it works exactly as you've explained.
 
Thomas Hauck
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback Matthew.

This example was from a mock exam, and I wanted to verify "why" the program generates this output.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic