Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does Java Overriding Work

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?

like image 549
Pramod Avatar asked Feb 24 '26 18:02

Pramod


1 Answers

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.

like image 76
Ajay S Avatar answered Feb 26 '26 08:02

Ajay S