Friday, 31 May 2013

Method Overriding

Method Overriding: Having same method in subclass as declared in parent class is known as method overriding.

If child class provides implementation for method which is already exist in it's parent class, it is known as Overriding.

Rules to override method:
  • Method must have same name as parent class method name.
  • Method must have same number/return type of parameters as in parent class.
  • Method must have same return type. Ex: if parent method return type is int, child class method type also should be int.
  • We can not throw or throws an exception if parent class did not do any of these.
  • We can not override static method. (Will explain later about this). 
  • Instance methods can be overridden only if they are inherited by the subclass.
  • A method declared final cannot be overridden.
  • A subclass in a different package can only override the non-final methods declared public or protected.
  • If a method cannot be inherited then it cannot be overridden.
  • Constructors cannot be overridden.
  • The access level cannot be more restrictive than the overridden method's access level. For example: if the super class method is declared public then the overridding method in the sub class cannot be either private or public. However the access level can be less restrictive than the overridden method's access level.
Advantages of Overriding:
  • Method overriding is useful to provide specific implementation to method which is provided by it's super class.
  • Method overriding is used for run time polymorphism
Example for Method Overriding:

*********************************************************
public class MethodOverridingExample {

public void overrideMethod() {
System.out.println("overrideMethod() of MethodOverridingExample");
}

public void testMethod() {
System.out.println("testmethod() of MethodOverridingExample");
}

}

*********************************************************
public class MethodOverridingDemo extends MethodOverridingExample {


//if we change return type of overriedn method in sub class we will get compile time exception
    //Cannot reduce the visibility of the inherited method from MethodOverridingExample
    //If we change the return type of overrided menthod we will get compile time exception
    //The return type is incompatible with MethodOverridingExample.overrideMethod()
    //If we try to throws an exception we will get compiltime exception
    //Exception Exception is not compatible with throws clause in //MethodOverridingExample.overrideMethod()


public void overrideMethod() {
System.out.println("overrideMethod() of MethodOverridingDemo");
}

public void demoMethod() {
System.out.println("demoMethod() of MethodOverridingDemo");
}

/**
 * @param args
 */
public static void main(String[] args) {

MethodOverridingDemo demo = new MethodOverridingDemo();

// Following line  calls the method of MethodOverridingDemo
demo.overrideMethod();

// Following line calls the method of MethodOverridingDemo
demo.demoMethod();

MethodOverridingExample example = new MethodOverridingExample();

// Following line calls the method of MethodOverridingDemo
example.overrideMethod();

// Following line calls the method of MethodOverridingDemo
example.testMethod();

// Here we are assigning the subclass object to super class reference
MethodOverridingExample example2 = new  MethodOverridingDemo();

// Following line calls the method of MethodOverridingDemo not
// MethodOverridingExample. Because it is overriden method. In this way we 
  //can achieve run time  polymorphism.  
  example2.overrideMethod();

// Following line causes the comiple time error because demoMethod() is
 // not overriden method and it is MethodOverridingDemo method.
 // So comment it.
// example2.demoMethod();

// following line calls the testMethod() of  MethodOverridingExample
example2.testMethod();
}
}

OutPut:

overrideMethod() of MethodOverridingDemo
demoMethod() of MethodOverridingDemo
overrideMethod() of MethodOverridingExample
testmethod() of MethodOverridingExample
overrideMethod() of MethodOverridingDemo
testmethod() of MethodOverridingExample

Explantion: In the above example you can see that the even though example2  is a type of MethodOverridingExample class but it runs the overrideMethod method in the MethodOverridingDemo class. The reason for this is : In compile time the check is made on the reference type. However in the runtime JVM figures out the object type and would run the method that belongs to that particular object.

*********************************************************
Why we can not override static method:
Because static method is bound with class where as instance method is bound with object. Static belongs to method area where as instance belongs to heap area.

Differences between Method Overloading and Overriding:
  • Overloading is used to increase the readability of program
  • Overriding is used to provide specific implementation for method which is exist in it's parent class.
  • Parameters must be different in overloading
  • Parameters must be same in overriding
  • Overloading is performed with in class.
  • Overriding occurs in two classes that have IS-A relation(Inheritance)

No comments:

Post a Comment