
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Overload or Override Static Method in Java
In Java, you cannot override static methods but you can overload them. Overloading a static method is allowed because the JVM determines which method to call at compile time based on the method signature, rather than the object's type. However, overriding is not allowed for static methods because an overridden method is called based on the object's type.
In this article, we are going to discuss is it possible to overload and override a static method in Java. Also, we will understand static methods, method overloading and method overriding.
What is a Static Method?
A method defined using the static keyword is known as Static method. Here, the static keyword is a non access modifier. A static method belongs to the class not to the instance of that class. Therefore, you don't need to create an object to call this method.
Example
Following example demonstrates a static method in Java.
public class MathOperation { // a Static method public static int addition(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { // method call using class int resOfCalc = MathOperation.addition(5, 3); System.out.println("Result after adding: " + resOfCalc); } }
Output of the above code is as follows:
Result after adding: 8
Overriding a Static Methods in Java
If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. The method name and parameters must be same.
As mentioned earlier, it is not possible to override a static method by another static method in sub-class. The reason behind this is that sub-class only hides the static method but not overrides it.
Example
If you try to override, the compiler will call the method that defined in the Parent class as shown in the below example:
class Parent { // a static method in parent class static void display() { System.out.println("Inside Static method of Parent class"); } } class Child extends Parent { // a static method in sub class static void display() { System.out.println("Inside Static method of sub class"); } } public class Main { public static void main(String[] args) { // creating object Parent obj1 = new Parent(); Parent obj2 = new Child(); // method calls obj1.display(); obj2.display(); } }
On running, you will get the following output:
Inside Static method of Parent class Inside Static method of Parent class
Overloading a Static Method in Java
If a class has multiple methods with the same name but different parameters, it is known as Method Overloading. Only the parameter should be different in case of overloading.
Example
The example of overloading a static method in Java is given below:
public class SuperClass { // a Static method with no parameters public static void display() { System.out.println("display()"); } // Method overloading of static method public static void display(int a) { System.out.println("display(int): " + a); } // Overloading static method with two parameters public static void display(String msg, int count) { for (int i = 0; i < count; i++) { System.out.println("display(String, int): " + msg); } } public static void main(String[] args) { display(); display(10); display("Tutorialspoint", 3); } }
Running the above code will give the following result:
display() display(int): 10 display(String, int): Tutorialspoint display(String, int): Tutorialspoint display(String, int): Tutorialspoint
Notes
The important points to remember:
The static method is resolved at compile time and cannot be overridden by a subclass. An instance method is resolved at runtime can be overridden.
A static method can be overloaded.