Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Can you override the static method in Java? if I create the same method in the subclass is it a compile-time error?

Sumit Rawal answered on August 28, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • main method in java without static keyword
  • java how to override a private method
  • What is the difference between static (class) method and instance method?
  • how to call a private method from another class in java
  • why main method is public static void in java
  • Can we override the static method?
  • do i have to use static methods in java main
  • static class java
  • Java Nested Static Class
  • why is the main method static in java
  • how to call a static method in java
  • Static Method in Java
  • c# override static method
  • how to call a static method from another class in java
  • Method Overriding in java
  • static method in non static class java
  • Calling Static Method in Java
  • what is method overriding in java
  • static class java
  • Java Static Inner Class
  • What is a Static Method? Why do we need Static methods in Java 8 Interfaces?
  • Why do we mark main method as static in Java?
  • Why Java does not allow overriding a static method?
  • can you override static methods in java
  • run static method java
  • instance method and static method

  • Can you override the static method in Java? if I create the same method in the subclass is it a compile-time error?

    0

    In Java, you cannot override a static method in the traditional sense, and if you create a method with the same signature in the subclass, it is not considered method overriding but rather method hiding.

    When you define a static method in a class, it is associated with the class itself, not with instances of the class. Therefore, static methods are resolved at compile-time based on the reference type, not the runtime object type. This means that if you create a method with the same name and signature in a subclass, it will not override the static method in the superclass; instead, it will shadow or hide it.

    Here's an example to illustrate this concept: 

    Popularity 1/10 Helpfulness 1/10 Language java
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on Aug 28 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.