Can you override Static Methods in Java?
来源:互联网 发布:上合组织 知乎 编辑:程序博客网 时间:2024/04/30 13:47
Can you override Static Methods in Java?
Question: Can you override Static Methods in Java?
Answer: Well... the answer is NO if you think from the perspective of how an overriden method should behave in Java. But, 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. Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it (which is the case with overriden static methods). Okay... any guesses for the reason why do they behave strangely? Because they are class methods and hence access to them is always resolved during compile time only using the compile time type information. Accessing them using object references is just an extra liberty given by the designers of Java and we should certainly not think of stopping that practice only when they restrict it :-)
Example: let's try to see what happens if we try overriding a static method:-
class SuperClass{
......
public static void staticMethod(){
System.out.println("SuperClass: inside staticMethod");
}
......
}
public class SubClass extends SuperClass{
......
//overriding the static method
public static void staticMethod(){
System.out.println("SubClass: inside staticMethod");
}
......
public static void main(String []args){
......
SuperClass superClassWithSuperCons = new SuperClass();
SuperClass superClassWithSubCons = new SubClass();
SubClass subClassWithSubCons = new SubClass();
superClassWithSuperCons.staticMethod();
superClassWithSubCons.staticMethod();
subClassWithSubCons.staticMethod();
...
}
}
Output:-
SuperClass: inside staticMethod
SuperClass: inside staticMethod
SubClass: inside staticMethod
Notice the second line of the output. Had the staticMethod been overriden this line should have been identical to the third line as we're invoking the 'staticMethod()' on an object of Runtime Type as 'SubClass' and not as 'SuperClass'. This confirms that the static methods are always resolved using their compile time type information only.
原文链接:http://geekexplains.blogspot.com/2008/06/can-you-override-static-methods-in-java.html
- Can you override Static Methods in Java?
- Can you override Static Methods in Java?
- Something about static Methods in C++ you should know
- The Default Methods And Static Methods In Java Interface
- Interface in Java 8(Default/Static methods)
- Question 16: Which of the following methods can a developer use to override the default terminate() function in C++?
- Can we Override Private Method in Java? Inner Class?
- Override in Java(Note)
- 17.5.4 Override methods
- 17.5.4 Override methods
- Source->Override/Implement Methods
- Can you sleep in storm night?
- Can you sleep in storm night?[2]
- you can only work in client 800
- Override and Overload in Java
- java static i hate you
- Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project.
- Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project.
- Ubuntu12.04 LTS下环境变量设置
- UVA 10177 (2/3/4)-D Sqr/Rects/Cubes/Boxes?
- Declare 用法
- LeetCode 70 — Climbing Stairs(C++ Java Python)
- 一篇很全面的freemarker教程
- Can you override Static Methods in Java?
- Android基于http协议和httpClient上传文件
- HDU 1009FatMouse' Trade
- Nios2 编译失败分析
- ios 学习之 debug和release版本的运行
- Linux系统中的文件目录结构介绍
- centos cvs 配置
- mysql 当字段值为NULL时,显示为特定值
- java编程题