Easy Tutorial
❮ Thread Status Java Hashmap Computeifabsent ❯

Java Override and Overload


Override

Override is the process where a subclass rewrites the implementation of a method that is accessible from its parent class, with the condition that the return value and parameters remain unchanged. The shell remains the same, but the core is rewritten!

The benefit of overriding is that the subclass can define its own behavior as needed. This means the subclass can implement a method from the parent class according to its needs.

An overridden method cannot throw new checked exceptions or broader exceptions than those declared by the overridden method. For example: If a method in the parent class declares a checked exception IOException, the overridden method cannot throw Exception as Exception is a superclass of IOException, but it can throw IOException or a subclass of IOException.

In the principles of object-oriented programming, overriding allows any existing method to be overridden. Here is an example:

TestDog.java File Code:

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

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

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

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

The above example compiles and runs with the following result:

Animals can move
Dogs can run and walk

In the example above, even though b is of type Animal, it executes the move method of the Dog class.

This is because during the compilation phase, only the reference type is checked.

However, at runtime, the Java Virtual Machine (JVM) specifies the type of the object and executes the method of that object.

Therefore, the above example compiles successfully because the move method exists in the Animal class, but at runtime, the method of the specific object is executed.

Consider the following example:

TestDog.java File Code:

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

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

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

      a.move(); // Executes the method in Animal class
      b.move(); // Executes the method in Dog class
      b.bark();
   }
}

The above example compiles and runs with the following result:

TestDog.java:30: cannot find symbol
symbol  : method bark()
location: class Animal
                b.bark();
                 ^

The program will throw a compile-time error because the reference type Animal does not have the bark method.


Rules for Method Overriding

-

Constructors cannot be overridden.

-

If a class cannot be inherited, its methods cannot be overridden.


Usage of the Super Keyword

When it is necessary to invoke the overridden method of the parent class in a subclass, the super keyword should be used.

TestDog.java File Code:

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

class Dog extends Animal{
   public void move(){
      super.move(); // Invokes the superclass method
      System.out.println("Dogs can run and walk");
   }
}

public class TestDog{
   public static void main(String args[]){

      Animal b = new Dog(); // Dog object
      b.move(); // Executes the Dog class method

   }
}

The above example compiles and runs with the following result:

Animals can move
Dogs can run and walk

Overloading

Overloading occurs within a class when methods have the same name but different parameters. The return types can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types.

The most common place for overloading is the constructor.

Overloading Rules:

Example

Overloading.java File Code:

public class Overloading {
    public int test(){
        System.out.println("test1");
        return 1;
    }

    public void test(int a){
        System.out.println("test2");
    }   

    // The following two have different parameter order
    public String test(int a,String s){
        System.out.println("test3");
        return "returntest3";
    }   

    public String test(String s,int a){
        System.out.println("test4");
        return "returntest4";
    }   

    public static void main(String[] args){
        Overloading o = new Overloading();
        System.out.println(o.test());
        o.test(1);
        System.out.println(o.test(1,"test3"));
        System.out.println(o.test("test4",1));
    }
}

Differences Between Overriding and Overloading

Aspect Overloaded Method Overridden Method
Parameter List Must be changed Must not be changed
Return Type Can be changed Must not be changed
Exceptions Can be changed Can be reduced or removed, must not throw new or broader exceptions
Access Can be changed Must not be more restrictive (can be less restrictive)

Summary

Method overriding and overloading are different manifestations of polymorphism in Java. Overriding represents polymorphism between a parent and a subclass, while overloading is a specific form of polymorphism within a class.

❮ Thread Status Java Hashmap Computeifabsent ❯