- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we overload or override a static method in Java?n
If a class has multiple functions by the same name but different parameters, it is known as Method Overloading. If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its superclass parameter must be different in case of overloading, the parameter must be same in case of overriding.
Now considering the case of static methods, then static methods have following rules in terms of overloading and overriding.
Can be overloaded by another static method.
Can not be overridden by another static method in sub-class. The reason behind this is that sub-class only hides the static method but not overrides it.
Following example demonstrates the same.
Example
class SuperClass { public static void display() { System.out.println("SuperClass.display()"); } //Method overloading of static method public static void display(int a) { System.out.println("SuperClass.display(int): " + a); } } class SubClass extends SuperClass { //Not method overriding but hiding public static void display() { System.out.println("SubClass.display()"); } } public class Tester { public static void main(String[] args) { SuperClass object = new SubClass(); //SuperClass display method is called //although object is of SubClass. object.display(); object.display(1); } }
Output
SuperClass.display() SuperClass.display(int): 1
Notes
The static method is resolved at compile time cannot be overridden by a subclass. An instance method is resolved at runtime can be overridden.
A static method can be overloaded.
- Related Articles
- Can we Overload or Override static methods in Java?
- Can we override a private or static method in Java
- Can we override the static method in Java?
- Can we overload Java main method?
- Can we override a protected method in Java?
- Can we override a start() method in Java?
- Can we overload the main method in Java?
- Can we override the equals() method in Java?
- Can we override the main method in java?
- Can I overload static methods in Java?
- Why can’t we override static methods in Java?
- Can we declare an abstract method final or static in java?
- Can we override only one method while implementing Java interface?
- Can we have a private method or private static method in an interface in Java 9?\n
- Can we declare a static variable within a method in java?
