Tuesday, 5 May 2015

static method overloading or overriding in Java

To set the context, first let's have a little introduction of what is overloading and what is overriding.

  • Method Overloading - When two or more methods with in the same class or with in the parent-child relationship classes have the same name, but the parameters are different in types or number the methods are said to be overloaded.
    Refer Method overloading in Java for better understanding.
  • Method Overriding - In a parent-child relation between classes, if a method in subclass has the same signature as the parent class method then the method is said to be overridden by the subclass and this process is called method overriding.
    Refer Method overriding in Java for better understanding.

Static method and overloading

Static methods can be overloaded just as 'instance methods'. So it is possible to have two or more static methods having the same name, but the parameters are different in types or number.

Example
public class StaticOverloadDemo {
 //Method with no param
 public static void TestMethod(){
  System.out.println("in TestMethod with no parameter");
 }
 //Method with one param
 public static void TestMethod(int i){
  System.out.println("in TestMethod with one int parameter");
 }
 //Method with two params
 public static void TestMethod(int i, int j){
  System.out.println("in TestMethod with two int parameters");
 }
 
 public static void main(String[] args){
  StaticOverloadDemo.TestMethod();
  StaticOverloadDemo.TestMethod(4);
  StaticOverloadDemo.TestMethod(5, 6);
  
 }
}

Output

in TestMethod with no parameter
in TestMethod with one int parameter
in TestMethod with two int parameters

Please note that -

  • Just like, with instance methods in case of static methods too methods which differ only in return type are not said to be overloaded.
  • Two methods with one as static and one normal instance method are also not considered to be overloaded methods
      // static
     public static void TestMethod(){
      System.out.println("in TestMethod with no parameter");
     }
     //non-static
     public void TestMethod(){
      System.out.println("in TestMethod with no parameter");
     }
    
    Thus any such attempt like the above code will throw compiler error.

Static method and overriding

Static methods can not be overridden in Java. Though it is possible to have a static method with same signature in sub-class but in that case sub-class method hides the super class method rather than overriding it. There won't be any run time polymorphism in that case as static methods are bound during compile time; they are not resolved at run time.

Example

//Super class
class ParentClass{
 //Static Method
 public static void displayData(){
  System.out.println("in displayData method of class ParentClass");
 }
 // non-static method
 public void displayValues(){
  System.out.println("in displayValues method of class ParentClass");
 }
}
//Derived class
class ChildClass extends ParentClass{
 // Not overridden but hiding the super class displayData method
 public static void displayData(){
  System.out.println("in displayData method of class ChildClass");
 }
 //overriding the super class displayValues method
 public void displayValues(){
  System.out.println("in displayValues method of class ChildClass");
 }
}

public class StaticOverrideDemo {
 
 public static void main(String[] args) {
  // Parent class Object with a reference of childclass
  ParentClass pc = new ChildClass();
  // no rum time polymorphism parent class method will be called
  pc.displayData();
  // this will call child class method
  pc.displayValues();

 }

}

Output

 
in displayData method of class ParentClass
in displayValues method of class ChildClass

It can be seen that even if the reference of the child class is assigned to the parent class object, parent class method is called not the child class displayData() method.
Where as for another method displayValues() run time polymorphism happens. If we need to call the displayData() method of the child class then we have to have an object of child class.

Points to note -

  • We can have static overloaded methods, which have same name but differ in types or number of parameters.
  • Static methods can not be overridden, any method with the same signature in sub-class will hide the super-class method not override it.
  • Static methods are resolved at compile time not run time thus overriding static methods is not possible.

That's all for this topic static method overloading or overriding in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. static in Java
  2. static import in Java
  3. Why main method static in Java
  4. interface static methods in Java 8
  5. static block in Java
  6. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

2 comments: