Java Overriding

overriding in java

In object-oriented terms, Java Overriding means to override the functionality of an existing method.

As we saw in the previous lesson, a subclass can define a behavior that’s specific to the subclass type, meaning that a subclass can implement a parent class method based on its requirement.This feature is known as method , overriding.

Method overriding is also known as runtime polymorphism.


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
   }
}

In the above example, you can see that even though b is a type of Animal it runs the move method in the Dog 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.

Therefore, in the above example, the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object.

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 void bark() {
      System.out.println(“Dogs can bark”);
   }
}
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
      b.bark();
   }
}

Rules for Method Overriding:

  • Should have the same return type and arguments
  • The access level cannot be more restrictive than the overridden method’s access level (Example: If the superclass method is declared public, the overriding method in the sub class can be neither private nor protected)
  • If a method cannot be inherited, it cannot be overridden
  • A method declared final or static cannot be overridden
  • Constructors cannot be overridden.

Using the super Keyword:

When invoking a superclass version of an overridden method the super keyword is used.

Example:

class Animal {
   public void move() {
      System.out.println(“Animals can move”);
   }
}
class Dog extends Animal {
   public void move() {
      super.move();   // invokes the super class method
      System.out.println(“Dogs can walk and run”);
   }
}
public class TestDog {
   public static void main(String args[]) {
      Animal b = new Dog();   // Animal reference but Dog object
      b.move();   // runs the method in Dog class
   }
}

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *