How Does Java Overriding Work

旧时模样 提交于 2019-12-01 23:20:27

问题


I have a stupid confusion, when we override the parent class method then does this derived overridden method still hold the code of parent class method, or it is a new fresh method which we can define?


回答1:


Read this article to get concept clear. http://docs.oracle.com/javase/tutorial/java/IandI/override.html

Generally we do when we want to extend the method of super class or to want to change the complete logic.

For ex: Super class have sorting method which use bubble sort.

In Derived class you want to take same method but want to implement quick sort. Then we do overriding.

Second

If you want to execute super class method first then your sub class overriden method logic then we use super.methodname().

Last to point of your question If you override the method and not called super class method like super.method() then its not mean its fresh method. Its means I already explain the sort example.




回答2:


what happens is exactly what's written in the annotation.

you override the method and instead of running the parent code for the method, it runs the current class's code



来源:https://stackoverflow.com/questions/14672144/how-does-java-overriding-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!