Showing posts with label Difference in overriding and overloading in java. Show all posts
Showing posts with label Difference in overriding and overloading in java. Show all posts

Monday, 14 December 2015

Java : Overriding and Overloading Concepts and Difference.

Hello friends, I know overloading/overriding are one of the famous OOB concepts that everyone knows JBut I feel more often people got confuse in this concepts.

Please go through the below diagram that will give you broad picture and lets understand point by point.




Overriding:

  • Overriding is runtime polymorphism
  • Happens in different class and Use in Inheritance : Overriding is concept that use in Inheritance where super class and sub class has same method name and same arguments
           Ex.
class Animal {
     public void myOverRiding ()
     {
      System.out.println("This is animal overriding ");
     }
}

class Horse extends Animal {
     public void myOverRiding ()
     {
      System.out.println("This is horse overriding ");
     }
}
  • Can we prevent overriding?
o   “Final” keyword can be prevent class to be inherited that means technically we are preventing overriding.
o   It will throw following compile time error.


  •  JVM decides what to do at run time: when you are calling any method with help of sub/super class object, JVM decides at run time that method of which class needs to be called
      public class UserAnimal {
     public static void main(String s[])
     {
           Animal aniHr =  new Horse(); //Line 4
           aniHr.myOverRiding();
   //In overriding it will call actual object class method.
     }
}

class Animal {
     public void myOverRiding ()
     {
           System.out.println("This is animal overriding ");
     }
}
class Horse extends Animal {
    
public void myOverRiding ()
     {
          System.out.println("This is horse overriding ");
     }
}

                                In above example we have two classes Animal – Super class and Horse – Sub class which shares same method called myOverRiding() now in UserAnimal class we have created an object of Horse and reference of Animal (refer Line # 4) and we called myOverRiding() method with help of that object (aniHr) now question is, which class’s method myOverRiding() will be called ? Any guess?

     Answer, in overriding JVM runtime use actual object and in overloading JVM use reference (Animal) instead of actual object (Horse)


O/P:  This is horse overriding

Overloading:

  • Overloading is compile time polymorphism
  • Happens in same class only
  • Method can have same name and different arguments
   class Animal { 
   MyStuff() {  System.out.println("Hi Cow");  }
   MyStuff(int i) { System.out.println("Hi Dog");  }
  }

  • Can we prevent override? Unlike override prevent with final keyword with class , java don’t have anything to prevent overload in class (unless you wish to not write same method name J )
  • JVM decides everything at compile time : which class method needs to be called that will decide at compile time (in overloading JVM use reference (Animal) instead of actual object (Horse))
  • If you try to write same method with same arguments it will throw compile time error.
Ex.  This is the similar example which I have explained above just o/p is different

public class UserAnimal {
     public void doStuff(Animal a)
     {
           System.out.println("This is animal ");
     }
     public void doStuff(Horse a)
     {
           System.out.println("This is horse");
     }
     public static void main(String s[])
     {
           UserAnimal obj = new UserAnimal();
           Animal aniHr =  new Horse();
           obj.doStuff(aniHr)
       //In overloading it will call reference class method.
     }
}
class Animal {
}
class Horse extends Animal {
}

O/P: This is animal

Hope this article would be value added and helpful, have a happy learning J

Thanks,
Sagar Vyas