Saturday, July 17, 2021

static Method Overloading or Overriding in Java

Can we overload static Method in Java and can we override static method in Java are important Java interview questions, in this tutorial we'll try to understand whether that is possible or not. To set the context for understanding static method overloading and static method overriding in Java, 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 overloading in Java

Static methods in Java 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.

Static method overloading Java 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 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 overriding in Java

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.

Static method overriding Java 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 in Java, which have same name but differ in types or number of parameters.
  • Static methods can not be overridden in Java, 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!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. static Import in Java With Examples
  2. static Block in Java
  3. Why main Method static in Java
  4. Interface Static Methods in Java 8
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. strictfp in Java
  2. Difference Between Abstract Class And Interface in Java
  3. throws Keyword in Java Exception Handling
  4. Exception Propagation in Java Exception Handling
  5. How ArrayList Works Internally in Java
  6. How to Loop Through a Map in Java
  7. CopyOnWriteArrayList in Java With Examples
  8. Dependency Injection in Spring Framework

2 comments: