Exforsys
+ Reply to Thread
Results 1 to 3 of 3

Can we override static methods in java?

This is a discussion on Can we override static methods in java? within the Java forums, part of the Programming Talk category; you don't get any compiler error if you try to override a static method. That means, if you try to ...

  1. #1
    jessie143143 is offline Junior Member Array
    Join Date
    Oct 2011
    Answers
    10

    Can we override static methods in java?

    you don't get any compiler error if you try to override a static method. That means, if you try to override, Java doesn't stop you doing that; but you certainly don't get the same effect as you get for non-static methods. Because they are class methods and hence access to them is always resolved during compile time only using the compile time type information.


  2. #2
    lokeshm is offline Member Array
    Join Date
    Apr 2005
    Answers
    83
    No, we cannot override static methods in Java. Methods are called based on the run time type of the object and not on the compile time type of it. If you try to override, there will be no compiler error however, you will not get same result as non-static methods. Static methods will be hidden when you are mistakenly overriding it. Hiding a method is not the same as overriding a method. Moreover, run-time polymorphism is applicable only for overriding a method not for hiding.


  3. #3
    Sandhya.Kishannag is offline Member Array
    Join Date
    Feb 2012
    Answers
    66
    Overriding is not supported as overriding depends on having an instance of a class. In polymorphism we can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass . A static method is not associated with any instance of a class so the overriding is not possible.


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...