Can we Overload or Override static methods in java ?

Overriding : Declaring a method in subclass which is already present in parent class is known as method overriding. This is also known as run-time polymorphism. Here a sub class or a derived class provides detailed implementation of a method which is already declared or defined in the parent class. The implementation to be executed is decided at run-time and decision is made according to the object used for calling the method. To read more about method overriding click here.

Overloading: Overloading is also a feature of OOP languages like Java that is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input parameters. To read more about Overloading Methods click here.

Can we Overload static methods?

Yes. We can have two or more static methods with same name, but differences in input parameters. For example, consider the Interger class of Java. Where we have different overloaded versions of parseInt() method.

static int parseInt(String s) : Parses the string argument as a signed decimal integer.

static int parseInt(String s, int radix) : Parses the string argument as a signed integer in the radix specified by the second argument.

Can we overload methods that differ only by static keyword?

We cannot overload two methods in Java if they differ only by the static keyword where the number of parameters and types of parameters are same of the two methods. Consider the following example:

public class Test {
    public static void foo() {
        System.out.println("Test.foo() called ");
    }
    public void foo() { // Compiler Error: cannot redefine foo()
        System.out.println("Test.foo(int) called ");
    }
    public static void main(String args[]) { 
        Test.foo();
    }
}

Can we Override static methods?

As the basic concept says, static method belongs to Class not instance or Object. A static method can be accessed directly by the class name and doesn’t need any object. If Class Student has a static method getName(), then we call it by below two ways:

1.  Student.getName();

And

1.  Student s;
2.  s.getName();

Now, Method overriding is done to achieve Run Time Polymorphism. An overridden method is called according to the object invoking it, not according to the reference type.

And we call the static methods using the class name, Static methods are looked up statically i.e. at compile-time. Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods.

We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class. This is also called method hiding. For example,

  Class Base {
     // Static method in base class which will be hidden in subclass 
     public static void display() {
        System.out.println("Static or class method from Base");
     }
  }

  // Subclass
  Class Derived extends Base {
     // This method hides display() in Base 
     public static void display() {
        System.out.println("Static or class method from Derived");
     }
  }
  public class Test {
   public static void main(String args[]) {
    Base obj1 = new Derived();
    // As per overriding rules this should call to class Derive's static 
    // overridden method. Since static method can not be overridden, it 
    // calls Base's display() 
    obj1.display();
   }

 

core java 12 Inheritance 12

FOLLOW US ON LinkedIn



Explore Tutu'rself