Method overriding in java: introduction

Method overriding is used when we want to define a method present in the parent class inside a subclass.

The two methods must have the same name and the same list of arguments:

  • Same order of arguments
  • Same types of arguments.
  • The return type should be assignment compatible (identical if it is a primitive type and subclass in the case of reference type) with the return type defined in the parent method. It can vary in the overloaded methods (not a good design choice).

The parent class contains generally a common behavior between its child classes.

Sometimes, the child class needs to change the behavior of the parent class.

This is where it comes the concept of method overriding. This concept allows changing the behavior of a method present in the parent class while keeping the name of the method defined in that parent class.

Method overriding in java example

We will show the code of the super class which the Employee class:

package sandbox;
public class Employee {
public void introduceYourself(){
System.out.println("I am an employee");
}
}

The code of the child class is the following:

package sandbox;
public class Doctor extends Employee {
 public void introduceYourself(){
 System.out.println("I am a doctor");
}         
}

The code of the main class is the following:

package sandbox;
public class TestInheritance {
public static void main(String[] args) {       
Doctor d = new Doctor();
d.introduceYourself();
}
}

The output of this program is the following:

I am a doctor

We notice that we have overridden the introduceYourSelf() method of the super class (Employee) in the child class (Doctor).

The method defined in the child class hides the correspondent method in the super class.

If we don’t override the method introduceYourSelf() in the child class, then the introduceYourSelf() method of the parent class will be called.

The method to call in the context of method overriding is determined at runtime. This is called the dynamic binding in opposition to the static binding.

In fact, in the context of the static binding, the method to call is determined at compile time.

The dynamic binding is related to the polymorphism. To know more about the polymorphism, please check the java polymorphism tutorial.

The method overriding in JAVA is a concept tightly related to inheritance unlike the method overloading in JAVA.

Method overriding in java rules

-static methods of the parent class cannot be overridden

-The methods present in the parent class and that can’t be accessed by the child class cannot be overridden:

  • private methods cannot be accessed outside the class where they are defined
  • final methods cannot be inherited

-We can’t decrease the visibility of the method of the parent class inside the child class:

  • If the method of the parent class is public, then we can’t put any access modifier other than public in the child class’s method.
  • If the method of the parent class is protected, then we can put the protected or the public access modifiers in the child class’s method.

- The method present in the child class cannot throw a checked exception which is not thrown in the parent class’s method.

- The method present in the child class can throw any unchecked exception.

This tutorial whose title is java method overriding arrives at its end.

Please don’t forget to subscribe to like our How to Program Facebook page or to subscribe to our newsletter to get our latest JAVA tutorials.

To check our core java tutorial, please check this link.

See you in the next core java tutorial.

Post a Comment

Previous Post Next Post