• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
Evacuate the building! Here, take this tiny ad with you:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic