• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

static method in parent class

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

I want to know what will be the out put and why it is so?

public class Merry {

static void doHelloGoodbye(Base b){
b.greeting();
b.farewell();
}

public static void main(String args[]){
Sub s = new Sub();
doHelloGoodbye(s);
}
}


class Base{
static void greeting(){
System.out.println("Hello");
}
void farewell(){
System.out.println("Goodbye");
}
}

class Sub extends Base{
static void greeting(){
System.out.println("Hi");
}
void farewell(){
System.out.println("See ya!");
}
}

Thanks in advance.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you will get an output of Hello,see ya, because you are assigning object ot Sub class to reference variable of Base class in doHelloGoodBye method,for static methods ,the method in class of referenc variable is called,in this example method in Base class would be called,where as for non static methods in the class whose object was prepared is called in your example it is method from Sub class.

so answers will be like that.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly kiran!!!
thats good explanation!!
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this thread
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kiran,

I understand the call to farewell() will invoke the version overridden in Sub call, but why is the call to static method greeting() will invoke the method defined in Base class(inspite of the fact that both the reference variable & the object are of the Sub class type). Is it because the static method in Base class is hiding the static method in sub class?.

Thanks in advance.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the parameter of the method doHelloGoodbye is of type Base, the reference is automatically upcast to a Base reference.
 
satya kiran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Your explanation is very helpful for me to understand the result.
reply
    Bookmark Topic Watch Topic
  • New Topic