Java Method Overriding

Java Method Override

Java Method Overriding


Java method overriding a concept in which we override base class method with the same signature to provide our own definition in child class. Method overriding is the part of dynamic binding or run time polymorphism. Method overloading happens only with inheritance. In this tutorial, we will discuss about method overriding and it’s related concepts with simple examples.

What is method overriding

When you define a method in child class with the same signature which is already present in base class, usually called method overriding. In method overriding, you need to create a method with same name, same parameter types and number of arguments. Method overriding is part of inheritance. Method overriding can not happen if child class don’t have any parent.

Let’s take a real world example

Let’s take an example of air plane. We all know that pilot uses two modes to fly a plane “Automatic Mode” and “Manual Mode”. In manual mode, plane flies as per the direction given by the pilot. In this mode, pilot is responsible for fly functionality. 

But when plane is flying in the air and stable. Pilot can now override the base functionality from manual to automatic mode. Now Automatic mode has functionality written to fly a plane as per GPS directions.

So as per given example. Plane is the base class which has fly method with manual mode as base functionality. But at a particular time, pilot uses automatic mode. So now, Automatic class is inheriting the fly method of base class Plane to provide it’s own definition to fly a plane in automatic mode.


Java Method Override



Some important pointer to remember about method overriding

  1. Signature of the method (Method name, parameter and data types) should be same.
  2. Method overriding can only happen in inheritance. You can not override same method in same class. Overriding class needs to be qualified for inheritance.
  3. Return type is not considered in method overriding.
  4. final methods do not qualify for method overriding.
  5. static methods do not qualify for method overriding.
  6. Overridden method will be called when overridden method is invoked by child class.
  7. Overridden method should be less restrictive than the method in the base class. Means, private method can be public. But public method can not be private.


Let’s create an example of method overriding

Let’s take an example of SmartPhones base class which has some methods with some default values. Each vendor has to extends the base class to provide their own definition or need to override the methods as below.

class SmartPhone {

      public double getScreenSize() {
           return 4.0;
      }

      public int numberOfSim() {
           return 1;
      }
}

class Samsung extends SmartPhone {

      public double getScreenSize() {
           return 5.5;
      }

      public int numberOfSim() {
           return 2;
      }
}

class Micromax extends SmartPhone {

      public double getScreenSize() {
           return 5.0;
      }

      public int numberOfSim() {
           return 2;
      }
}


In above example, you can see SmartPhone has two methods getScreenSize() and numberOfSim() with some default values. But they may be different from vendor to vendor. So each vendor has to extends the base class and provide their own settings.


Why we can not override final methods

We say that, we can not override final methods. Why we can not override final methods, what is the reason behind it. Simple explanation to this question is that when final is applied to the method. Those methods are hidden to sub classes. This is a simple sign that do not override this method which is used to prevent it from unexpected behavior from the subclass.

Why we can not override static methods

We know that static is related to class rather than object in java. If you say static that means, we are talking about Class not an object. In inheritance, child class acquired all the methods and properties of the base class by parent class object.

But in case of static, object is not in the picture. Hence, static method will not be known by the child class. So override is not possible.

Can we add more restriction to overridden methods in child class

No, you can not apply more restrictions to the overridden method. Only less restrictions are allowed as per the java standard. If you will try to restrict more, you will get error at that time.

Leave a Reply

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