//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???/