Java – Method overriding

Home/Java - Tutorial/Java - Inheritance/Java – Method overriding
Java – Method overriding 2017-06-13T00:12:36+00:00

Method overriding in java:

Defining a method in the subclass that has the same name, same arguments and same return type as a method in the superclass and it hides the super class method is called method overriding.

Now when the method is called, the method defined in the subclass is invoked and executed instead of the one in the superclass.


 


Output :
super y = 200
sub z = 100
Here the method display() defined in the subclass is invoked.

Overloading VS Overriding:

Method overloading is compile time polymorphism.

Method overriding is run time polymorphism.

Overloading a method is a way to provide more than one method in one class which have same name but different argument to distinguish them.

Defining a method in the subclass that has the same name, same arguments and same return type as a method in the superclass is called method overriding.

Example of overriding is as above.

For example of overloading CLICK HERE

Prev Next