• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Shadowing static methods

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code compiles and apparently is an example of shadowing a static method. It sure looks like a static method overwriting another static method to me.
Does anyone have any insights?
public class A
{
public static void sM1() { System.out.println("In base static"); }
}
class B extends A
{
public static void sM1() { System.out.println("In sub static"); }

}
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but you're not overriding it -- you're hiding it -- if you were overriding it, you'd be able to call super.sM1()... but you can't:

[ July 12, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a question from my mock exam that illustrates the difference between overriding an instance method and hiding a static method. Which answer do you believe is correct?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compiled and ran this program, I get a totally unexpected result... it is P.prints1 Q.prints2. Can you please explain me.
Thanks
Charu
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are known at compile time itself.




prints1() method of P class is done at compile time.
Therefore, printS1S2() invokes prints1() of P class.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charu,
Yes, I can explain the results.
This question from my mock exam is an example of the differences between overriding inherited instance methods and hiding static methods of the super class. The explanation appears below the code.

Class Q inherits method printS1S2 from class P. The method printS1S2 invokes methods printS1 and printS2. Since printS1 is static the reference to it is assigned at compile time and therefore P.printS1 is invoked. However, printS2 is an instance method so the reference to it is not computed until run time when printS1S2 is invoked as an inherited member of class Q. As a result, Q.printS2 is invoked.
Method Q.printS1 hides P.printS1. However, Q.printS2 overrides P.printS2.
If you would like to work with more questions ralated to inheritance, then you might want to try my mock exam at the URL contained in my signature. On the home page, you will see two versions of the exam, July 4 and July 8. You should always use the latest version. After you click on July 8, then go to the topic exams and select inheritance. There are 10 questions in the inheritance topic and all of them have explanations.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can a non static method printS1S2() call Static method printS1();?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
non-static methods, i.e. instance methods can call static methods, but not the other way round.
i.e. static methods can only call other static methods.
 
Charu Murali
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan. Now I got it.
Charu.
 
John Held
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all!
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic