Table of contents
1.
What is Overriding in Java?
2.
What is Overloading in Java?
3.
Can we Override Static Methods in Java?
4.
Can we Overload the Static Method in Java?
5.
Can we Overload Methods that Differ Only by Static Keyword?
6.
Frequently Asked Questions
6.1.
Which is better: Overloading or overriding?
6.2.
What are the two types of Overloading and Overriding?
6.3.
Is it possible to override or overload a static method in Java?
7.
Conclusion
Last Updated: Nov 6, 2024
Medium

Can we Overload or Override static methods in java?

Author Gunjan Batra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

No, static methods in Java cannot be overridden because static methods are associated with the class itself rather than an instance of the class. When a subclass inherits a static method from its parent class, it is not possible to override the behaviour of that static method. They cannot be overridden because they do not act on a specific instance of an object. 
In Java, static methods can be overloaded but not overridden. They can have different parameters while having the same name in the same class or subclass. They cannot be overridden because they act on the class itself, not an object.

Can we Overload or Override static methods in java

Let’s take a real-life example of overloading and overriding methods in object-oriented programming concepts. Assume you are supposed just to perform the function of talking. Say you have to tell the story of your day to a total stranger. Your function will get over pretty quickly. Say, now you are telling the same to your beloved. You will go through more details as compared to the previous one. What has happened here is, that you have performed the same function, but based on the parameter stranger/beloved, your way of implementing the function changed! This is called an overloading method.

Now let’s look into another sample. We learn a lot from our parents! We learn to cook to some extent. Your mother prepares the same delicacy with the same ingredients with a different taste, and you with a different taste (assuming). That is overriding, same function (cooking), same parameters (ingredients), but different algorithms (cooking style). Or, you learned driving from your dad! But you both drive the same vehicle differently! That is overriding.

Overloading and Overriding Static Methods in Java


The above picture is one of the best examples of method overloading and overriding. Let’s see the implementation and differences to understand the concept better.

Also read about Iteration Statements in JavaDuck Number in Java

What is Overriding in Java?

The method overriding is re-defining a method in a child class that is already defined in the parent class. When both parent and child classes(or derived classes) have the same method(same name, parameters, and same return type), that method is said to be the overriding method. The method overriding enables the child class to change the method acquired from the parent class according to its requirement. 

In the case of the method overriding, the method binding happens at run time. The method binding which happens at run time is known as late binding. So, the method overriding follows late binding. The method overriding is also known as dynamic method dispatch or run time polymorphism or pure polymorphism

Code implementation:

//Super Class
class ParentClass{
    int num = 10;
    void showData() {
        System.out.println("Inside ParentClass showData() method");
        System.out.println("num = " + num);
    }
}
//Sub class
class ChildClass extends ParentClass{
    void showData() {
        System.out.println("Inside ChildClass showData() method");
        System.out.println("num = " + num);
    }
}
//Main Class
public class Main {
    public static void main(String[] args) {
        ParentClass obj = new ParentClass();
        obj.showData();
        obj = new ChildClass();
        obj.showData();
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:


Inside ParentClass showData() method
num = 10
Inside ChildClass showData() method
num = 10

You can also read about Java Destructor and Hashcode Method in Java.

What is Overloading in Java?

Method overloading is a concept of Java in which we can create multiple methods of the same name in the same class, and all methods work in different ways. When more than one method of the same name is created in a Class, this type of method is called the Overloaded Method.

Overloading is also a feature of Object-Oriented programming language concepts like Java and C++ that is related to compile-time (or static) polymorphism. Method overloading allows.

Also see, Swap Function in Java

Can we Override Static Methods in Java?

The shortest answer is No, static methods in Java can’t be overridden. If a derived class defines a static method with the same signature as a static method in the base class, the method in the derived class is hidden by the method in the base class. While overriding a method, we must follow the below list of rules.  

  • Static methods can not be overridden.  
  • Final methods can not be overridden.  
  • Private methods can not be overridden.  
  • A constructor can not be overridden.
  • Use the super keyword to invoke the overridden method from the child class. 
  • The return type of the overriding method must be the same as the parent has it.  
  • The access specifier of the overriding method can be changed, but the visibility must increase but not decrease. For example, a protected method in the parent class can be made public, but not private, in the child class. 
  • If the overridden method does not throw an exception in the parent class, then the child class overriding method can only throw the unchecked exception, throwing a checked exception is not allowed.  
  • If the parent class overridden method does throw an exception, then the child class overriding method can only throw the same or subclass exception, or it may not throw any exception.

Can we Overload the Static Method in Java?

Yes, we can overload static methods, we can have two or more static methods with the same name but with different parameters. Let’s look into the implementation of overloading static methods in Java.

Code implementation:

public class Main {
public static void fun() {
System.out.println("CodingNinjas");
}
public static void fun(int a) {
System.out.println("CodingNinjas(int)");
}
public static void main(String args[])
{
Main.fun();
Main.fun(10);
}
}
You can also try this code with Online Java Compiler
Run Code


Output:

CodingNinjas
CodingNinjas(int)

 

In the above code, we used the same name for two functions but using different parameters. As we can see, the code is executed and working properly.

You can also find the output of this java compiler code here.

Can we Overload Methods that Differ Only by Static Keyword?

We cannot overload two methods in Java if they differ only by static keyword given the number of parameters and types of parameters is the same.
Code implementation:

public class Main {
public static void fun() {
System.out.println("CodingNinjas");
}
public static void fun() {
System.out.println("CodingNinjas(");
}
public static void main(String args[])
{
Main.fun();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Main.java:6: error: method fun() is already defined in class Main

Must Read Static Blocks In Java and Fibonacci Series in Java.

Frequently Asked Questions

Which is better: Overloading or overriding?

Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.

What are the two types of Overloading and Overriding?

Overloading and overriding are two types of Polymorphism. Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism.

Is it possible to override or overload a static method in Java?

It is possible to overload a static method in Java, but it is not possible to override a static method. Overloading a static method means having multiple methods with the same name but with different parameter lists within the same class or subclass. 

Conclusion

In this blog, we discussed the Overriding and Overloading methods in Java. We tried to implement code for the static methods in this category. At last, we discussed overloading methods that differ only by static keyword.

Recommended Reading: 

Access modifiers in Java

Solid Principles in Java

Refer to our guided paths on Code360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our coursesrefer to the mock test and problems look at the interview experiences and interview bundle for placement preparations.

Live masterclass