Can we override static method in java

No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods.

On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time.

Below are the reasons why we can’t override static method in java:-

  • Static methods are those which belong to the class.They do not belong to the object and in overriding, object decides which method is to be called.
  • Method overriding occurs dynamically(run time) that means which method is to be executed decided at run time according to object used for calling while static methods are looked up statically(compile time).

When you run above program, you will get below output.

Hello…Good morning
Hello…Good morning

As per the rules of method overriding, method call is resolved at run time by the type of object.So, in our above example d.hello() in second example should call hello() method of DisplayMessage class because reference variable of Display class is referring an object of DisplayMessage but it call Display class hello() itself.This happens because static method is resolved at compile time.

So If derived class’s static method have same signature as base class’s static method, it is called method hiding not method overriding.

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Table of ContentsEarly Binding or Static BindingWhy are static final and private methods associated by Early Binding?Code Example:Analysis:Late Binding or Dynamic BindingCode Example:Output:Analysis:Differences between Static and Dynamic Binding Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 28 July

    Inheritance in Java

    Table of ContentsIntroductionBasic SyntaxExample of Inheritance in JavaTypes of Inheritance in JavaSingle InheritanceMulti-Level InheritanceHierarchical InheritanceMultiple and Hybrid InheritanceWhy does Java not support Multiple Inheritance?Conclusion In this post, we will see about Inheritance in java. It is one of the OOPs principles apart from Abstraction, encapsulation and polymorphism. Introduction The word Inheritance is quite familiar with […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.