
- Forum
- Programming Talk
- Java
- Can we override static methods in java?
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 ...
-
10-24-2011, 06:42 AM #1
- 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.
-
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.
-
02-22-2012, 07:28 AM #3
- 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

Reply With Quote





