Thursday 5 April 2012

Method Overriding

2. Dynamic Polymorphism (Method Overriding):
          Defining a method with same method signatures in sub class is known as Method Overriding.

        Class SuperClass{
                 public String override(int i, String s) {
                      System.out.println("SuperClass Override method called");
                 }                
         }

        Class SubClass extends SuperClass{
                 public String override(int, String s) {
                      System.out.println("SubClass  Override method called");
                 }
         }


        Class Test{
                 public static void main(String[] args) {
                      SuperClass sc = new SubClass ();
                      sc. override(1, "hello");
                 }
         }

Output:
SubClass  Override method called

Conditions for method overriding are:
1.Firstly, we need to inherit the super class members (inherits variables and methods)  in order to override the method in sub class.
2. SubClass method signatures should be same to that of SuperClass method.
3. method return types must be same in both the methods.
4. If SuperClass method throws any exception the SubClass method too should throw the same exception or its sub class type exception

Important Note:
1. Method return type is part of a Overriding.
2. SubClass method access modifier should be atleast same type or more widening to that of SuperClass method.

private > default (no modifier or package level) > protected > public
Above are the access modifiers widening from left to right.

3. We can not override private method as they are not inherited into sub class.
4.  In above program we are creating a SubClass object and holding it in a SuperClass reference. ie., super class reference referring to a sub class object.
now when we call sc.override(1, "hello") method the SubClass method will be called rather than SuperClass method.

5. Here SuperClass reference will invoke the SubClass method. When we are trying to invoke any method specific to SubClass only. i.e. method not present  in SuperClass, it will give compile time error.

6. We can not override static methods, by doing so it leads to a new concept called as Method Hiding.
Here in method hiding when we call sc.override(1, "hello") method the SuperClass method will be called rather than SubClass method. ie., SubClass method will be hidden.

7. final methods can not be overriden. To avoid overriding use "final" keyword before return type in the super class method.


But why overriding is considered as dynamic polymorphism?
     Because the method invocation ( sc.override(1, "hello") ) call is not binded to the respective method at compile time itself by the compiler as compiler doesn't know which class method to invoke and leaves it to JVM to invoke and at runtime JVM invokes SubClass method. Here as the binding is happening at run time it is also know as late binding or dynamic binding or run time binding.

2 comments:

  1. Thanks for your comment on my blog post What is polymorphism in Java, I see you have also done a good Job. I did share my view on Difference between method overriding and overloading in Java you may like. By the way add follow button your blog so that reader can subscribe it and welcome to blogging world.

    ReplyDelete