Sunday, April 29, 2018

Can we override static method in java?

No ! we cannot override the static methods. Since static method belongs to class level not object level.

class StaticExe1 {
public static void run() {
System.out.println("run of StaticExe1");
}
public  void fly() {
System.out.println("fly of non-static Exe1");
}
}

class StaticExe2 extends StaticExe1 {
public static void run() {
System.out.println("run of StaticExe2");
}
public  void fly() {
System.out.println("fly of non-static Exe2");
}
}
class StaticExe3 extends StaticExe2 {
public static void run() {
System.out.println("run of StaticExe3");
}
public  void fly() {
System.out.println("fly of non-static Exe3");
}
}

public class StaticMethod {

public static void main(String[] args) {

StaticExe2 ex = new StaticExe3();
ex.run();
ex.fly();
}


}
Output:
run of StaticExe2

fly of non-static Exe3

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...