Static Methods Overloading and Overriding in Java are some of the important terms related to object-oriented programming. This feature is used to implement polymorphism with the help of Java programming. The above question, Can we Overload or Override static methods in Java? is on the most important list of questions asked by the interviewer.
We will go through the answers in brief, covering all the important points in this article. In this article, we will cover the problem questions related to two important methods in Java, Overloading and Overriding.
You can also refer to the complete path on the PhysicsWallah website and app to help you learn more about the DSA program and know the best approach for effective preparation. And, PhysicsWallah also provides mock tests and interview preparation courses to help you prepare for your placement.
Static Method
A static method is a method in which a class has the keyword static kept before its name. When a method is declared static, it means that the method is associated with the class itself rather than with a specific object or any instances of the class.
It is also called a class-level method or class method. We do not need to create an object for the class, as we can call it directly using the class name.
Public static int sum()
{ } |
Static methods cannot be used to access the instance methods or variables, as they belong to the class and not to the instances of it.
Overriding Method
In object-oriented programming, overriding is a method that is used to achieve run-time polymorphism. Let us look a little more into this term overriding.
Basically, inheritance is a basic feature in object-oriented programming in which one class can inherit properties and behaviors from another class. When a child class inherits from a parent class, it automatically gets access to all the methods defined in the parent class.
Method overriding occurs when the subclass provides its own specific implementation of a method that already exists in its parent class.
To perform method overriding, the parent and child classes must have the same name and the same input parameters in their methods.
The choice of which version of a method to execute is made at run-time when you create an object of the subclass and call a method that is also present in the parent class.
Overloading
Method Overloading is a concept in object-oriented programming that allows us to define multiple methods in a class with the same name but different parameter lists.
This means we can have several methods with the same name, but each method can take different types or numbers of parameters.
When we call a method that is overloaded, the decision about which version of the method to execute is made by the compiler at compile-time based on the arguments that we pass to the method.
Now that we understand what method overloading and overriding mean, let us move on to our question’s objective.
Can We Override Static Methods In Java?
No, we cannot override static methods in Java. It is not an actual override when a subclass defines a static method with the same name and signature as a static method in the parent class. Instead, the parent class method is covered up by the subclass method.
The following guidelines should be kept in mind when overriding methods in Java:
- First, keep in mind that static methods cannot be overridden in Java.
- The same applies to final methods, which are those that the parent class has declared with the “final” keyword.
- We cannot override private methods in the parent class as they are not accessible in the subclass. Hence, there is no concept of overriding private methods.
- Constructors cannot be overridden because they are special methods used to create and initialize objects.
- To invoke the overridden method from the child class, we can use “super” keyword.
- The return type of the overriding method must be the same as the return type of the parent’s method.
- The visibility of the access specified of the overriding method can be changed in the subclass, but it must not be more restrictive than the visibility of the parent method.
- Example: A protected method from the parent class cannot be made private in the child class but can be made public.
- The overriding method in the child class can only throw unchecked exceptions (subclasses of RuntimeException) if the parent method does not throw any exceptions.
It is not allowed to throw checked exceptions (subclasses of Exception) that the parent method has not already thrown.
- If the parent method throws an exception, the overriding method in the child class can throw the same exception or its subclasses, or it may choose not to throw any exceptions at all.
Can We Overload Static Methods In Java?
Yes, we can overload static methods in Java. Method overloading in Java allows us to have multiple methods with the same name in a class, but they must have different parameter lists.
The Java compiler identifies which method to call based on the number or types of arguments passed to the method.
Overloading Static Methods in Java |
public class Main {
// Static method with no parameters public static void fun( ) { System.out.println(“PhysicsWallah”); } // Static method with an integer parameter public static void fun(int a) { System.out.println(“PhysicsWallah with int parameter: ” + a); } // Main method public static void main(String[] args) { // Calling the overloaded static methods Main.fun(); Main.fun(5); } } |
Output
PhysicsWallah
PhysicsWallah with int parameter: 5 |
There are two classes called Main,” each having two static methods named “fun.” The first method, fun, takes no parameters and simply prints “PhysicsWallah.” However, the second method prints “PhysicsWallah with int parameter:” followed by the value of the integer after taking an integer as a parameter. The first iteration of the method is executed when we call the “fun” method without any arguments.
The second iteration of the “fun” method, which accepts an integer parameter, is called when the argument “5” is passed to the method call.
Static method overloading enables us to write more adaptable and flexible code that can handle various input types without changing the method names. This makes it possible for Java programs to have cleaner, shorter code.
Also Read Technical Topics
FAQs
Can static methods be overridden in Java?
No, Java does not support overriding static methods. The parent class method is not actually overridden when a subclass defines a static method with the same name and signature as a static method in the parent class.
What is the major difference between overloading and overriding?
The concept of overloading allows for the existence of multiple methods in the same class or subclass with the same name but different parameter lists. It is resolved at compile-time based on the method signature.
In contrast, overriding takes place when a subclass offers a unique implementation of a method that already exists in its parent class. Depending on the object type, it is resolved at runtime.
Is it possible to override and overload a static method in Java?
Yes, it is possible to overload a static method in Java, but it is not possible to override it. While overriding is applicable to static methods, overloading allows you to have multiple static methods with the same name but different parameters in the same class or subclass.
Which one is better, Overloading or overriding in Java?
There are situations where overloading and overriding are appropriate. The flexibility of handling various types or numbers of parameters within the same class is made possible by overloading, which enables more adaptable methods. On the other hand, overriding enables subclasses to achieve polymorphism by customizing method behavior's.
What are the two main types of Polymorphism in Java?
Static and dynamic polymorphism are the two types of polymorphism used in Java. Static polymorphism is demonstrated by method overloading, where the choice of which method to call is made at compile time based on the method signature.
Dynamic polymorphism is showed by method overriding, where the choice is made at runtime based on the actual type of the object being referenced.