BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Can Static Methods be Overloaded or Overridden in Java?

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this guide, we will discuss whether we can overload or override a static method in Java.

Prerequisites: To understand this article, you should have the basic knowledge of following topics.

  • Method Overloading in Java
  • Method Overriding in Java
  • Static Methods in Java

Can we overload a static method?

Short answer is ‘Yes’. We can overload a static method. In the following example, we are overloading a static method myMethod().

public class JavaExample {
  //static method overloading
  public static void myMethod() {
    System.out.println("Hello, Welcome to beginnersbook.com!");
  }
  public static void myMethod(String str) {
    System.out.println("Hello "+str+ " glad you are here.");
  }
  public static void main(String args[])
  {
    JavaExample.myMethod();
    JavaExample.myMethod("Chaitanya");
  }
}

Output:

Hello, Welcome to beginnersbook.com!
Hello Chaitanya glad you are here.

Can we overload a non-static method with a static method?

If both the methods have same signature but one method is non-static and the other method is static then, this is not a valid case of method overloading and it would throw a compilation error.

public class JavaExample {
  //static method
  public static void myMethod() {
    System.out.println("Static Method.");
  }

  //non-static method with the same name and parameters
  public void myMethod() {
    System.out.println("Regular Method.");
  }

  public static void main(String args[])
  {
    //calling method
    JavaExample obj = new JavaExample();
    obj.myMethod();
  }
}

Output:
Error while overloading a non-static method with a static method

Can we Override static methods in Java?

No, we cannot override a static method. However when we try to override a static method, the program runs fine without any compilation error, it is just that the overriding doesn’t take place. Instead of calling the derived class method, the compiler invokes the base class static method, it is because static methods cannot be overriden.

You can say that when we define a static method with the same signature in derived class which is present in the base class, then the static method of base class hides the same signature method of derived class, this concept is called method hiding.

// Base Class
class Parent {

  // Static method of base class
  public static void hello() {
    System.out.println("Base class says hello!");
  }

  // Non-static method of base class
  public void bye()  {
    System.out.println("Base class says bye!");
  }
}

// Derived class
class Child extends Parent {

  // Here we are trying to override the hello() method of base
  // class, however it cannot be done, thus this method will be hidden
  // and cannot be called.
  public static void hello() {
    System.out.println("Child class says hello!");
  }

  // Overriding non-static method of base class
  public void bye() {
    System.out.println("Child class says bye!");
  }
}
//Main class
public class JavaExample {
  public static void main(String args[ ])  {
    Parent obj = new Child();

    // Unlike normal methods, static methods can't be overriden
    //so this would call the static method of base class
    obj.hello();

    // For normal methods, overriding is perfectly fine.
    //This should call the method of derived class
    obj.bye();
  }
}

Output:
output

Recommended Posts

  • Method Overloading with Auto-boxing and Widening in Java
  • Ambiguity error while overloading method with Varargs
  • Difference between non-static and static methods
  • Java 8 Interface changes – default method and static method
  • Method Overloading vs Method Overriding in Java
❮ Java Programming

Top Related Articles:

  1. How to write to file in Java using BufferedWriter
  2. Constructor Overloading in Java with examples
  3. Static and dynamic binding in java
  4. Java – Static Class, Block, Methods and Variables
  5. Passing a List to a Varargs method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap