Are all method in java implictly virtual

前端 未结 3 1727
滥情空心
滥情空心 2020-12-14 08:11

If there are no compile time binding in java, do this mean all binding are done at runtime?

But as per OOPs concept for runtime binding, functions must have virtual

相关标签:
3条回答
  • 2020-12-14 08:57

    All non-static, non-final and non-private methods are virtual by default in Java. However JVM is clever enough to find classes having only one implementation of given method and turn it into static binding.

    This way you don't have to remember about virtual keyword (ever experienced memory leak due to missing virtual on destructor in C++?) while the performance is not impacted that much.

    0 讨论(0)
  • 2020-12-14 08:59

    Non-static method invocation is the main (only) dynamic aspect of Java. All methods are virtual in Java. This does not apply to static methods, which are bound at compile time, based on the static type of object.

    0 讨论(0)
  • 2020-12-14 09:11

    Methods which we can't override in sub class are generally called non virtual methods.

    In Java static, private & final methods are non virtual by default. Other methods are virtual by default.

    0 讨论(0)
提交回复
热议问题