Method Overriding in Java

Method Overriding in Java


If the child class in a program has a method that has a same name as that of the parent class then this will be call method overriding in Java programming language.

In other words we can say if the child class or the sub class in a program has same implementation as in the parent class then this will be called method overriding in java programming language.


Usage of Java method overriding:

In Java programming language we use the method overriding to provide same specific implementation as done in its super or parent class. Method overriding in java programming language is also used to achieve the run time polymorphism.


Rules of Java method overriding:

There are some rules in Java programming language that the programmers should abide by when using java method overriding:

  1. To override methods in Java programming language, the name of the methods in the child class should be the same as in its parent class.
  2. To override methods in Java programming language, the parameters of the methods in the child class should be the same as in its parent class.
  3. To override methods in Java programming language, the methods must be a IS- A relationship that is inheritance.


Understanding the problem without method overriding:

Consider the following example in which it is demonstrated that what will happen if we don’t use method overriding in our program:

CODE:

class Automobile {

void run () {

system. out. println (“Automobile is running”);

}

}

class Car extends Automobile {

public static void main (string [] args) {

Car obj = new Car ();

obj. run ();

}

}

OUTPUT:

Automobile is running

In the above example, the problem is that the method run () that is declared in the parent class should have a specific implementation in the child class or sub class. This is the reason why we use method overriding in the program.

In the code above we have two classes, the parent class is named as “Automobile” and the child class is named as “Car”. The object calls the method of the parent class and the method displays the output using print statement.


Example of method overriding:

Consider the following example in which we have defined the run () method in both of the classes that is in the parent class and in the child class. The run () method in both of the classes has some specific implementation:

CODE:

class Automobile {

void run () {

system. out. println (“Automobile is running”);

}

}

class Car extends Automobile {

void run () {

system. out. println (“Car is running”);

}

public static void main (string [] args) {

Car obj = new Car ();

obj. run ();

}

}

OUTPUT:

Car is running

In the above example, two classes are declared one is the parent class named “Automobile” and second is the child class named “Car”. Both of these functions have a method named run (). The run () method has same parameters and has same implementation. As the name of the method and the parameters of the method are same therefore, there is “IS- A” relationship between the classes. Hence we can say that there is method overriding in the program above.


Real example of java Method overriding:

Consider that you have a class that is named as Animal and now you have three child classes that fall in the category of animal. For example you can take cat, dog, Lion as the child classes and Animal as parent class. Now in all of the child classes you have a display () method that will have the same implementation and that will have same parameters. Consider the following diagram:

real-example-of-java-method-overriding

Now consider the following code in which we have implemented the above scenario:

CODE:

class Animal {

void display () {

system. out. println (“Animals that are mammal”);

}

}

class Cat extends Animal {

void display () {

system. out. println (“Cats are mammal”);

}

}

class Dog extends Animal {

void display () {

system. out. println (“Dogs are mammal”);

}

}

classLion extends Animal {

void display () {

system. out. println (“Lions are mammal”);

}

}

class ABC {

public static void main (string [] args) {

Cat c = new Cat ();

Dog d = new Dog ();

Lion l = new Lion ();

  1. display ();
  2. display ();
  3. display ();

}

}

OUTPUT:

Cats are mammal

Dogs are mammal

Lions are mammal

In the above example Animal is the parent class and cat, dog, and lion are the child classes. We used method overriding in the above program and the method display () is overridden. Now how the compiler will know that which method should be called. This problem is solved by creating the objects of all the classes and then calling the specific method using the corresponding object. Note that the implementation and name and the parameters of the function or method that is overridden is the same. In this way we got the required output.


Can we override static method?

A static method in Java programming language cannot be overridden. We can prove it by using the run time polymorphism.


Why we cannot override static method?

A static method in Java programming language is bound with classes and the instance methods in java programming language is bound with the object. Static in java programming language belongs to the class area only and the instance belongs to the heap area.


Can we override java main method?

In Java programming language the main method cannot be overridden because it is static. The main method in java is declared as follows:

public static void main (string [] args)


Difference between method overloading and overriding in Java:

The following are the main differences between method overloading and method overriding in java programming language:

Method Overloading Method Overriding
In Java programming language, method overloading is used to increase the readability of the program. In java programming language the method overriding is used to provide a specific implementation of the method whose implementation has already been done in its parent class or super class.
In java or other programming languages the method overloading is done within a class. In Java programming language method overriding is occurred in two classes and these have IS- A relationship. Also called inheritance.
Parameters should be different when performing method overloading in java programming language. Parameters should be same in method overriding.
We can achieve compile time polymorphism by using method overloading. We can achieve run time polymorphism by using method overloading.
In java programming language the method overloading is achieved by changing the parameters of the methods or functions and not the name or return type of the methods or functions. Return type of the methods however can be the same or different in method overloading. But parameters must be changed. The return type of the methods must be same in order to perform method overriding.

Java method overloading example:

Consider the following example in which we have implemented method overloading:

CODE:

class ExampleOverloading {

static int add (int x, int y) {

return x + y;

}

static int add (int x, int y, int z) {

return x + y + z;

}

}

It can be seen in the above code that the parameters of the methods are different and hence we achieved method overloading and it is done within one class only.


Java method overriding example:

class Animal {

void display () {

system. out. println (“Animals that are mammals”);

}

}

class Cat extends Animal {

void display () {

system. out. println (“Cats are mammals”);

}

}

It can be seen in the above example that there are two classes which has same methods. The name and parameters of the methods are same and hence we performed method overriding successfully. Each method of the class will be called by using its object.