Method Overriding in Java

In Method overriding if subclass is having same method as base class then it is known as method overriding Or in another words, if subclass provides specific implementation to any method which is present in its one of parents classes then it is known as method overriding.

Rule of Method Overriding

We say Method overriding if it shares these features with one of its superclass method:

  1. The same name.
  2. The same number of parameters.
  3. The same type of parameters.
  4. The same return type.
Use of Method Overriding

The key benefit to method overriding is that we can alter the behavior of the method inherited from a superclass and define its specification according to the need of the subclass.

Why method overriding is needed?

Method overriding is mainly used in a situation when we want to create a more specialized version of a generic(general) method inherited from a superclass, within the subclass.

Simple Example of Method Overriding

Below we have simple code example with one parent class and one child class wherein the child class will override the method provided by the parent class.

		class X 				//superclass
		{
		public void name()		//method name()  class X	
		{
		System.out.println(“Raj”);
		}
		}
		class Y extends X		//superclass of X
		{
		public void name()		//method name() of class X is overridden in class Y
		{
		System.out.println(“Abhishek”);
		}
		}

In above example, class X is inherited by class Y. The method name() of class X is returning Raj but when this method is inherited and overridden by class Y, it is returning Abhishek.

Example 2
		class Car{  
		void run(){System.out.println("Car is running");}  
		}
		class Bike extends Car{    //Creating a child class  

		//defining the same method as in the parent class
		void run(){System.out.println("Bike is running safely");}  
		public static void main(String args[]){  
		Bike obj = new Bike();  //creating object
		obj.run();  //calling object
		}
		}
Output

Bike is running safely

In above Example we define run method in child class as define in the parent class but it has specific implementation. Name and parameter are the same and here IS-A relationship between classes so there is method overriding.

Example of real scheme of java method overriding

Hear the real scheme of method overriding, there is office which provide salary to employees. The salary will give according to their designation. Example Developer, Designer, DBA(Data Base Administrator) could give 25000, 20000, and 30000.

		class Office{		//creating parent class
		int getsalary(){return 0;}
		}
		//creating child class 
		class Developer extends Office{
		int getsalary(){return 25000;}
		}

		class Designer extends Office{
		int getsalary(){return 20000;}
		}
		class DBA extends Office{
		int getsalary(){return 30000;}
		}
		//Salary class to create objects and call the methods
		class Salary{
		public static void main(String args[]){
		Developer a=new Developer();
		Designer b=new Designer();
		DBA c=new DBA();
		System.out.println("Developer Salary: "+a.getsalary());
		System.out.println("Designer Salary: "+b.getsalary());
		System.out.println("DBA Salary: "+c.getsalary());
		}
		}
Output

Developer salary 25000

Designer Salary 20000

DBA salary 30000

Copyright 1999- Ducat Creative, All rights reserved.