Tuesday, 9 December 2014

What is method overriding ?

Method Overriding:

Having the same method in the subclass as declared in the parent class is known as method overriding. If a subclass provides a specific implementation of a method that is already provided by its super class, it is known as Method Overriding.In simple if a super class and subclass has same
method name known as method overriding.

Advantage of Method Overriding:

>Method Overriding is used to provide specific implementation of a method that is already
   provided by its super class.
>Method Overriding is used for Runtime Polymorphism

Rules for Method Overriding:

>1. method must have same name as in the parent class
>2. method must have same parameter as in the parent class.

See the problem without mehtod overriding:

Let's see the problem that we may face in the program if we don't use method overriding.
Problem without method overriding:

class Car {
void run()  {  System.out.println("Car is running");}
}
class  Honda extends Car{
public static void main(String []args){
Honda obj = new Honda();
obj.run();
  }
}

Output: Car is running

Note:Problem is that I have to provide a specific implementation of run() method in subclass but we can not change its implementation thats why we have to use method overriding here.

Example of method overriding:

In this example, we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes, so here is method overriding.

class Car{
void run() {System.out.println("Car is running");}
}
class Honda extends Car{
void run() { System.out.println("Honda is running safely");}

public static void main(String args[]){
Honda obj = new Honda();
obj.run();
   }
}

Output: Honda is running safely

Ques) Can we override static method?

Ans) No, static method cannot be overridden.

Ques)Why we cannot override static method?

Because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.

Difference between method Overloading and Method Overriding:

There are three basic differences between the method overloading and method overriding. They are  as follows:

    Method Overloading:                           Method Overriding:

>Method overloading is  used             >Method overriding is used to provide
   to increase the readability                  the specific implementation of the method
   of the program.                                   that is already provided by its super class.
>Method overlaoding is performed   >Method overriding occurs in two classes that
  within a class.                                      have IS-A relationship.
>In case of method overloading        >In case of method overriding parameter must
  parameter must be different.              be same.

                                         
                             
           
                             

No comments:

Post a Comment