No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods. But we can declare static method with the same signature in the subclass and it is not consider override it is method hiding concept.
public class OverrideSuper {public static void hello() {System.out.println("In super class method");}}class OverrideSubClass extends OverrideSuper{@Overridepublic static void hello() {// The method hello() of type OverrideSubClass must override or// implement a supertype methodSystem.out.println("In sub-class method");}}
class OverrideSuper {public static void hello() {System.out.println("In super class method");}}public class OverrideSubClass extends OverrideSuper{public static void hello() {System.out.println("In sub-class method");}public static void main(String[] args) {hello();}}// In sub-class method
The above code is method hiding concept in java
No comments:
Post a Comment