What is method overriding and method overloading and give the scenario where it is used.

Showing Answers 1 - 4 of 4 Answers

chinnu

  • Nov 18th, 2006
 

Method overloading is writing the sane method parameters and different signatures. Method overriding is the wiriting the same method and same signatures. Whenever programmer want to add the new featurs to the same method programmer go for method overloading. This is the code refinement whenever programmer want to change the complete features to the same method then programmer go for method overriding. This is the code replacement.

  Was this answer useful?  Yes

Antony Praveen Kumar

  • Dec 1st, 2006
 

Method over loading : Compile time the compiler will decide which method to execute.(Example for compile time polymorphism)

Method over ridding : At run time OS will decide which method to execute.(Example for Runtime polymorphism)

  Was this answer useful?  Yes

Deepti Kajjam

  • Feb 22nd, 2007
 

Overriding vs. Overloading

Method overriding requires the same method signature (name and parameters) and the same return type. Only non-final instance methods in the superclass that are directly accessible from the subclass are eligible for overriding.

Overloading occurs when the method names are the same, but the parameter lists differ. Therefore, to overload methods, the parameters must differ in type, order, or number. As the return type is not a part of the signature, having different return types is not enough to overload methods.

Invoking an overridden method in the superclass from a subclass requires special syntax (e.g., the keyword super). This is not necessary for invoking an overloaded method in the superclass from a subclass.

 If the right kinds of arguments are passed in the method call occurring in the subclass, the overloaded method in the superclass will be invoked.

Example

class Light
{
    protected double getBill(int noOfHours) throws InvalidHoursException
    {
    ....
    ....
    }

}

class Tubelight extends Light
{

   public double getBill(final int noOfHours)throws  ZeroHoursException //Overriding     {
    ......
    ......
    }
    public double getBill()   //  Overloading
     {                    
      ......
      ......
    }
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions