Method overriding

To achieve Run-time polymorphism we use method overriding

There are some rules of method overriding

  • Methods  must present in the same class
  • Methos should have the same name
  • Methos should have different argument     
    • No of argument
    • Type of argument
    • Sequence of argument
  • Inheritance
  • If the method is final it can’t be overridden.
  • A method declared static cannot be overridden but can be re-declared.

If our program satisfy above point then we can achieve Run-time polymorphism

Overriding example:

class Animal {
   public void move() {
      System.out.println("Animals can move");

class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");

public class TestDog {

   public static void main(String args[]) {
      Animal a = new Animal();   // Animal reference and object
      Animal b = new Dog();   // Animal reference but Dog object

      a.move();   // runs the method in Animal class
      b.move();   // runs the method in Dog class

We have two class Animal and Dog

Dog class Extend Animal Class

Both class have a method with same name Move();

These methods have the same no of argument

We can override methods by making the object of  both class and call methods by there object


Important Link