Update appNew update is available. Click here to update.

Can we Overload or Override static methods in java?

Gunjan Batra
Last Updated: Jan 30, 2023
MEDIUM
Can we Overload or Override static methods in java

Introduction

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, 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.

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:

//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();
    }
}


Output:


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

Overloading in Java and make it h2

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 

Can we Override Static Methods in Java?

The accurate answer is No, static methods 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 super keyword to invoke 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 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:

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);
}
}


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:

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();
}
}


Output:

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

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.

Can we Override and Overload Static Method in Java?

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.

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. 

Can you override a static method in Java?

It is not possible to override a static method in Java, as they are bound to the class and not associated with an object. They cannot be overridden because they do not act on a specific instance of an object.

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.

Refer to our guided paths on Codestudio 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.

Happy Coding!

Was this article helpful ?
0 upvotes