What is Method Overriding in Java ??
When a base class extends a super class and both the base class and the super class have a method with same name, same types of parameters and same number of parameters, then the two methods are said to be overriden.
Points To Remember
  • Overriding of methods is always done in a base class or in other words, we always override a parent class method.
  • The access specifier of the overriden method can not be less than the parent class method's access specifier e.g if a parent class method is marked as protected, you can override it with access specifiers protected and public only.
  • Overriden methods should have same number of arguments and of same type.
  • The return type of the overridden method should be same as the overriding method.
  • We cannot override a static methods.
  • We cannot override private methods of parent class in a base class, it will give a compile time error if we try to do so.
Example Of Overriding
The following is a simple example of overriding.
class Base {
  public Base(){
     System.out.println("Base Constructor");     
  }
  public void show() {
     System.out.println("Base show");     
  }
}
   
class Derived extends Base {

  public Derived(){
     System.out.println("Derived Constructor");     
  }
  public void show() {  // overrides the Base's show()
     System.out.println("Derived show");  
  }
  public static void main(String[] args) {
      Base obj = new Derived();
      obj.show();
  }  
}
The above example will give an output
Base Constructor
Derived Constructor
Derived show
This is because the super class is always referenced before a sub class is referenced. Thus the Base Constructor is referenced first and the show method of Base class is made available to the JVM. Now the JVM references the Derived class and again finds the show() method. Thus it will keep the show() method of the Dervired class and whenever the class to show() method is made, the JVM will class the show() method of the Derived class.
Overriding Use Case
Suppose we want to make a function speak() that returns how a particular animal speaks e.g dog barks , ducks quack, cats meow.
We can do this in the following ways.
  • We can have a speak() method inside each class Dog, Cat or Duck and we can access the speak() method by making object of that class. This method is a bad practice and must be avoided.
  • We make a super class Animal and all other classes like Dog, Cat etc extend this class. Now we can give reference of the Animal class to all the objects of Dog, Cat , Duck etc.
class Animal {
  public void speak(){
     System.out.println("My language is not known"); 
  }
}

class Cat extends Animal {
  public void speak() {
     System.out.println("I am a cat and i do Meow Meow");     
  }
}

class Dog extends Animal {
  public void speak() {
     System.out.println("I am a dog and i do Woof Woof");     
  }
}

class Duck extends Animal {
  public void speak() {
     System.out.println("I am a duck and i do Quack Quack");     
  }
}

class Alien extends Animal {
}

class Test {

  public static void main(String[] args) {

     Animal dog = new Dog();
     Animal cat = new Cat();
     Animal duck = new Duck();
     Animal alien = new Alien();
     speak(dog);
     speak(cat);
     speak(duck); 
     speak(alien);  
  }  

  public static void speak(Animal animal){
   animal.speak();
  }


}
Output of the above example is as follows
I am a dog and i do Woof Woof
I am a cat and i do Meow Meow
I am a duck and i do Quack Quack
My language is not known
In the above example the method speak() of class Test takes the parameter animal object and prints what does that animal speaks. This method does not know about what type of animal it is having. Its only gets to know at runtime.

This is how Polymorphism(Dynamic Binding) works and this example shows how you can achieve polymorphism with the help of Method Overriding.


No comments :

Post a Comment