• 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

anotherone from Dan's

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}
}
Result is: P.printS1 Q.printS2
Could anybody explain it? Thanks
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
printS1 is not overriden(static method can not be overriden but rather, hide-ed/hidden) whereas printS2 is overriden.
And thus dynamic binding is applied to printS2 and not to printS1.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but...
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
void printS1S2(){printS1();printS2();} //added by me
public static void main(String[] args) {
new Q().printS1S2();
}
}
result is "Q.printS1 Q.printS2". Why?
[ May 22, 2003: Message edited by: Catherine Jones ]
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my understanding is that:
In the first case, the printS1S2 method is not overriden, when you have new Q().printS1S2();, it actually refers to the one in class P and it will envoke class P's printS1 method.
In the second case, the printS1S2 is overriden. when you have newQ().printS1S2(); it envokes its own(Q's) printS1S2 method and Q's static method printS1 is envoked as P's is hide-ed/hidden.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the first case, the printS1S2 method is not overriden, when you have new Q().printS1S2();, it actually refers to the one in class P and it will envoke class P's printS1 method.


if this is the reason then y is Q's printS2 invoked. P's printS2 shud have been invoked!


In the second case, the printS1S2 is overriden. when you have newQ().printS1S2(); it envokes its own(Q's) printS1S2 method and Q's static method printS1 is envoked as P's is hide-ed/hidden.

 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if this is the reason then y is Q's printS2 invoked. P's printS2 shud have been invoked!


see my previous post? printS2 method is properly overriden and dynamic binding applies whereas printS1 is NOT overriden......
[ May 22, 2003: Message edited by: Yi Meng ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is how I understand it:
Rule: Whenever a non-static method is inherited in a sub-class, any static references made in this method will resolve to implementations in the super class while any non-static references made in this method will resolve to implementations in the sub-class.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method Q.printS1 hides method P.printS1. Method Q.printS2 overrides method P.printS2. Therefore, method P.printS1S2 invokes P.printS1 and Q.printS2.
Please see Section 8.6.4 of the Java Language Specification.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In first case, printS1S2 method is inherited. Therefore a call to a static method within it will resolve to super-class and call to non-static method will resolve to sub-class.
In second case, printS1S2 method is overriden. Therefore both calls to static and non-static method is resolved to sub-class.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic