class Bird {
{
System.out.print("b1 ");
}
public Bird() {
System.out.print("b2 ");
}
}
class Raptor extends Bird {
static {
System.out.print("r1 ");
}
public Raptor() {
System.out.print("r2 ");
}
{
System.out.print("r3 ");
}
static {
System.out.print("r4 ");
}
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
/*Raptor rap = new Raptor();
if (rap instanceof Raptor){
System.out.println("i m working");
}*/
}
}
Guess what is the answer: r1 r4 pre b1 b2 r3 r2 hawk
static always related with class not with instance so they run when the class initialized, next turn for constructor but here we have init blocks so first the init blocks have run then the constructor come in picture from top to bottom, super class init => constructor => child class init => constructor...
share some interview advanced level questions related to some Open source : Linux, Apache, Mysql, Php, Java.
Showing posts with label override the static function. Show all posts
Showing posts with label override the static function. Show all posts
Monday, December 6, 2010
Friday, July 30, 2010
Java Static Function
Q. Can we override the Static function in Java ?
A. Yes we can override the Static function in Java see the sample recipe
class Sample implements Over {
public static int simple(int a, int b) {
int c = a + b;
return c;
}
}
class Overloading extends Sample {
public static int simple(int a, int b) {
int c = a * b;
return c;
}
public static void main(String[] args) {
Sample sam = new Sample(22, 90);
Overloading obj = new Overloading();
obj.simple(10,20); // here the sub class function call
sam..simple(10,20); // here the super class function call
}
}
A. Yes we can override the Static function in Java see the sample recipe
class Sample implements Over {
public static int simple(int a, int b) {
int c = a + b;
return c;
}
}
class Overloading extends Sample {
public static int simple(int a, int b) {
int c = a * b;
return c;
}
public static void main(String[] args) {
Sample sam = new Sample(22, 90);
Overloading obj = new Overloading();
obj.simple(10,20); // here the sub class function call
sam..simple(10,20); // here the super class function call
}
}
Subscribe to:
Posts (Atom)