Overloading and Overriding Static Methods in Java

Overloading and Overriding Static Methods in Java
Overloading and Overriding Static Methods in Java

Introduction

Let’s take a real-life example for 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.





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

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.

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 languages concepts like Java, C++ that is related to compile-time (or static) polymorphism. Method overloading allows 

Can we overload static methods?

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.

blog banner 1

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.

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.

What are the methods overriding and method overloading?

In method overloading, methods or functions must have the same name and different signatures. Whereas in the method overriding, methods or functions must have the same name and same signatures. Method overloading is an example of compile-time polymorphism.

What is the advantage of function overloading in Java?

Method overloading increases the readability of the program. This provides flexibility to programmers so that they can call the same method for different types of data. This makes the code look clean.

What is Overloading and Overriding in C++?

Overriding is needed when the derived class function has to do some added or different job than the base class function. Overloading is used to have the same name functions which behave differently depending upon parameters passed to them.

Key Takeaways

In this blog we discussed the Overriding and Overloading methods in Java, we tried to implement code for the static methods in this category.

  • In this blog, we discussed overriding static methods and followed by overloading static methods.
  • At last, we discussed overloading methods that differ only by static keyword.

Visit here to learn more about methods of Overloading and Overriding. And practice similar problems on CodeStudio. If you liked this blog, share it with your friends.

By: Dharani Mandla