One of the most frequently asked interview questions in Java is: Can we overload static methods in Java? Can we override static methods in Java? These questions often confuse developers, yet understanding them is essential for mastering object-oriented programming concepts.
In this tutorial, we’ll explore the rules and limitations of static method overloading and static method overriding in Java. To set the stage, we’ll first revisit the basics of method overloading and method overriding in Java, and then dive into how these concepts apply specifically to static methods.
- 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
In Java, static methods can be overloaded just like instance methods. This means you can define two or more static methods with the same name, as long as their parameter lists differ in type or number. Overloading static methods works exactly the same way as overloading non-static methods, and it is resolved at compile time.
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. While it is possible to declare a static method with the same signature in a subclass, this does not constitute overriding, it is known as method hiding. In such cases, the subclass method hides the superclass method rather than overriding it.
The key reason is that static methods are bound at compile time, not at runtime. Therefore, there is no run time polymorphism with static methods. Unlike instance methods, the method call is resolved based on the reference type, not the object type.
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
You may also like-
Method Overloading in Java
ReplyDeleteOh, great explanations
More about....Oops Cocept
ReplyDeleteLing