• 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

overriding question

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Question :
//The following code resides in a class file called Son . java .
// What happens when the file is compiled & run ?

class Father {

static String getName ( ) {
return " Father " ;
}

String getMsg ( ) {
return " Hello " ;
}
};

class Son extends Father {

static String getName ( ) {
return " Son " ;
}

String getMsg ( ) {
return " Bye " ;
}

public static void main ( String args [ ] ) {
Father ob = new Son ( ) ;
System .out . println ( ob. getMsg ( ) + " " + ob . getName ( ) ) ;
}
};

/*
Options :

a . Compiler error
b . Execption thrown at runtime
c . Prints " Bye Father " when run
d . Prints " Bye Son " when run
e . Prints " Hello Father " when run
f . Prints " Hello Son " when run
*/
/*I said option a thinking that SuperClass ref= new SubClass(); and
*ref.SubClassMethod(); will give error. But look closely, the getName( )
*and getMsg( ) method are overridden. So look towards 'new'.
* ob.getMsg() and ob.getName( ) must invoke methods from class
*Son. But ist invokes method from Father and 2nd call invokes
*method from Son. Why???/
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1:Static methods look on references
2:instance methods look on objects

hence the answers
check out with removing the static modifier from the code
and observe the answers ,u will get it wat the first two lines mean
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SO is the output :
Bye Father..

Reply appreciated..
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
first of all "static methods cant be overridden"
the method getName() is called hidden method not overridden method
when u call hidden method "it is the type of the reference" which decides which method to invoke.

and getMsg() is genuinely overridden in ur code and when u call overridden method "it is the instance type" which decides which method to invoke.

according to above rules, the output should be "bye,father"

thanx
 
reply
    Bookmark Topic Watch Topic
  • New Topic