Method Overriding in Java

Method Overriding is concept of overriding a same method in the child class. This is fall under the dynamic polymorphism  or run time polymorphism. A method defined in the parent class is considered as the common behaviour and the child classes are expected to override the common behaviour with own implementation. There are multiple restrictions enforced while overriding a method in the child class.

  • When you override a method, the method signature must be same as in the parent class. The method name and parameters has to exactly match for both the methods. The order also should be same.
  • The return type of the method has one exception, if a parent class method returns an object, child class can return same object or its sub class. Otherwise the code will not compile. In the below example, if parent class returns Employee object, child class can return Manager or Admin object. This feature is introduced from Java 5.0 release.
  • Static methods can not be overridden. Since method overriding is related to the object binding, static methods can not use this concept. Because static method is bound with class whereas instance method is bound with object.

With the above explanations, look at the below example for more details on how to implement method overriding.

Employee.java

package javabeat.net.core;

public class Employee {
	public void print(String name){
		System.out.println("Employee Name : " + name);
	}

	//Override with different return type
	public Employee getInstance(){
		return new Employee();
	}
}

Manager.java

package javabeat.net.core;

public class Manager extends Employee{
	public void print(String name){
		System.out.println("Manager Name : " + name);
	}
	public Manager getInstance(){
		return new Manager();
	}
}

Admin.java

package javabeat.net.core;

public class Admin extends Employee{
	public void print(String name){
		System.out.println("Admin Name : " + name);
	}
	public Admin getInstance(){
		return new Admin();
	}
}

OverrideDemo.java

package javabeat.net.core;

public class OverrideDemo {
	public static void main (String args[]){
		Employee employee = new Employee();
		Manager manager = new Manager();
		Admin admin = new Admin();

		//Directly calling methods
		employee.print("Nakulan");
		manager.print("Nakulan");
		admin.print("Nakulan");

		//Assign child class object to parent class reference
		employee = manager;
		employee.print("Nakulan");

		employee = admin;
		employee.print("Nakulan");

	}
}

Output of the above example will be:

Employee Name : Nakulan
Manager Name : Nakulan
Admin Name : Nakulan
Manager Name : Nakulan
Admin Name : Nakulan

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!