Wednesday 2 November 2011

Method overriding in Java


Method overriding in java

Overriding is a concept where instead of inheriting a method from its superclass,  a subclass can override the method by providing its own functionality to it. That is, subclass and superclass have the same method but outputs are different.  Overriding is useful on occasions when we want an object to respond to the same method but have different behavior when that method is called.  

The overridden method in the subclass has the same name, same arguments and same return type as the method in super class. Then, when that method is called, the method defined in subclass is invoked and executed instead of the one in the superclass.

If the superclass and the subclass have the same method, we say the superclass method is overridden by subclass. It is called method overriding. In case  of method overriding, the subclass prefers to call its own method.

1 comment:

  1. Method overriding in java means a subclass method overriding a super class method.STC Technologies

    ReplyDelete