2018-04-29

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();
}


}

No comments:

Post a Comment