• 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

static methods Help plz..

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
The output of this program is :
Goodnight,Dick.
Please explain ...
Vandanam,
vijay
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 statictest {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the static methods called on the object are in the class to which it belongs, not where it is referenced
e.g.
class duper{
static String greeting() { return "Super duper"; }
String name() { return "Anna"; }
}
class Super extends duper{
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class statictest {
public static void main(String[] args) {
duper s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
this code would print Super duper, Dick
K Singh
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
static methods cannot be overridden , its bcos they r meant for the class and not for the object.Even if u override by giving the same siganature the compiler does not takes it as a overriden methods but as a seperate method . This is resolved at compile time itself so there is no late binding. that is y u get that output
HTH
sunil.s
------------------
"Winners don't do different things
They do things differently"
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijaykumar,
static methods can't be overriden. So when you invoke a static method using a superclass's reference, the superclass version
is going to be called.
Hope this helps.
Regards
-------
vadiraj

------------------
*************************
There's a lot of I in J.
*************************
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic