• 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

Invocation of Hidden Class Methods

 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I found this example in JLS 8.4.8.5.I still couldn't able to understand why the output is "Goodnight, Dick" instead of "Hello Dick".
It would be more helpful if you could give me a detailed explanation.
<h4>

<code>
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
produces the output:
Goodnight, Dick
</code>


</h4>
Thanks
Balaji

[This message has been edited by Balaji Loganathan (edited July 22, 2001).]
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Balaji:
You should distinguish between class method and instance method. Instance method comes after object instance, while class method always comes with Class name. In this example, name() is an instance method, which comes with the object instance <code>new Sub()</code>. The <code>greeting()</code> is class method, which will come directly with Super.
Hope it helps.
Guoqiao

Originally posted by Balaji Loganathan:
Hi,
I found this example in JLS 8.4.8.5.I still couldn't able to understand why the output is "Goodnight, Dick" instead of "Hello Dick".
It would be more helpful if you could give me a detailed explanation.
<h4>



<code>
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
produces the output:
Goodnight, Dick
</code>


</h4>
Thanks
Balaji
[This message has been edited by Balaji Loganathan (edited July 22, 2001).]


 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balaji:
Because Sub greeting() hides Super greeting(), and Sub name()overrides Super name(), this kind of shadowing produces the result that you see rather than what you expected. Also the type reference used here is if type Super, not Sub. Please note the overriding here applies to this regular instance methods as it always does.
Hope this helps,
Joseph

Originally posted by Balaji Loganathan:
Hi,
I found this example in JLS 8.4.8.5.I still couldn't able to understand why the output is "Goodnight, Dick" instead of "Hello Dick".
It would be more helpful if you could give me a detailed explanation.
<h4>



<code>
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
produces the output:
Goodnight, Dick
</code>


</h4>
Thanks
Balaji
[This message has been edited by Balaji Loganathan (edited July 22, 2001).]


 
Balaji Loganathan
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guoqiao Sun and JOSEPH BIH now i understood(90%) the context.
Regards
Balaji
 
reply
    Bookmark Topic Watch Topic
  • New Topic