Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Method Overloading in Java


If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  1. Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  2. Method overriding is used for runtime polymorphism

12 Rules of Method Overriding in java

  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).

Rule #1:Only inherited methods can be overridden.

Because overriding happens when a subclass re-implements a method inherited from a superclass, so only inherited methods can be overridden, that’s straightforward. That means only methods declared with the following access modifiers: public, protected and default (in the same package) can be overridden. That also means private methods cannot be overridden.


The Dog class overrides both the move() (public) and eat() (protected) methods from the Animal class (regardless of packages where the both classes are declared):

public class Animal {
public void move() {
// animal moving code...
}
protected void eat() {
// animal eating code...
}
}
public class Dog extends Animal {
public void move() {
// dog moving code...
}
protected void eat() {
// dog eating code...
}
}
//example, the Dog class perfectly overrides the move() method which is declared with default access modifier in the Animal class, as long as both the classes are in the same package: package java64bit.com;
public class Animal {
void move() {
// Animal moving code...
}
}
public class Dog extends Animal {
void move() {
// Dog moving code...
}
}
//the Dog and Animal classes are in different packages. Thus it isn’t considered an overriding because the Dog class does not inherit the Animal’s move() method:
package java64bit.com.animal;
public class Anim
void move() {
// Animal moving code...
}
}
package java64bit.com.dog;
import net.codejava.core.animal.Animal;
public class Dog extends Animal {v void move() {
// Dog moving code...
}
}
//Here, the Dog’s move() method is just a new method, not an overriding one.
the Animal’s move() method is private, so the Dog’s move() method is just a new method, not an overriding one:
public class Animal {
private void move() {
// Animal moving code...
}
}
public class Dog extends Animal {
public void move() {
// Dog moving code...
}
}



Understanding the problem without method overriding

Let's understand the problem that we may face in the program if we don't use method overriding.

//Java Program to demonstrate why we need method overriding
//Here, we are calling the method of parent class with child
//class object.
//Creating a parent class
class CretaCar{
void run()
{
System.out.println("CretaCar is running to the high speed");
}
}
//Creating a child class
class Bike extends CretaCar{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}

output
CretaCar is running to the high speed


Problem is that I have to provide a specific implementation of run() method in subclass that is why we use method overriding.


Example of method overriding

In this example, we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes, so there is method overriding.

//Program to illustrate the use of Java Method Overriding
//Creating a parent class.
class CretaCar{
//defining a method
void run(){
System.out.println("CretaCar is running to the Safety the Road in the Main side");
}
}
//Creating a child class
class Bike extends CretaCar{
//defining the same method as in the parent class
void run(){
System.out.println("Bike is running safely on the Road");
}
public static void main(String args[]){
Bike obj = new Bike();//creating object
obj.run();//calling method
}
}


Output - Bike is running safely on the Road


Real example of Java Method Overriding

Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7% and 9% rate of interest.

//Creating a parent class.
class Bank{
int getRateOfInterest(){return 0;
}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){
return 12;
}
}
class ICICI extends Bank{
int getRateOfInterest(){
return 41;
}
}
class AXIS extends Bank{
int getRateOfInterest(){return 22; }
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}



Output -
SBI Rate of Interest: 12
ICICI Rate of Interest: 41
AXIS Rate of Interest: 22

Can we override static method?

No, static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.


Why we cannot override static method?

because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.


Can we override java main method?

No, because main is a static method.


Difference between method Overloading and Method Overriding in java

Click me for difference between method overloading and overriding


More topics on Method Overriding (Not For Beginners)

Method Overriding with Access Modifier

Let's see the concept of method overriding with access modifier.

Exception Handling with Method Overriding

Let's see the concept of method overriding with exception handling.


Copyright 2017 Design& Development