Overriding Method

It is sub class defines a method having same signature means the name, return type and argument list match exactly as a method of a parent class then sub class is said to be overriding methods of its parent class. It is one of the mean of implicating polymorphism.

The following rules of Method Overriding :

  • The subclass new method defination must have the same method name, the type and the number of parameters as well as order of parameters.
  • The return type must be same or covariant return.
  • Overridden methods cannot be less Accessible but can widen it.

Usege of Method Overriding :

  • invoking super class method
  • Dynamic method dispatch (Runtime polymorphism)
Note :
  • Overridden methods cannot be less Accessible
  • super class consturctor can not be overriden
  • final method can not be overriden.
  • static method can not be overriden.
Example of Overriding Method

import java.util.*; class Employee { // instance fields int employeeId; String name; double salary; Date hireDay; public Employee(int employeeId, String n, double s, int year, int month,int day) { this.employeeId=employeeId; name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); hireDay = calendar.getTime(); } public void getDetail() { System.out.println("Employee Id : "+ employeeId); System.out.println("Name : "+ name); System.out.println("Salary : "+ salary); System.out.println("hireDay : "+ hireDay); } } class Manager extends Employee { String department; public Manager(int employeeId, String n, double s, int year, int month,int day,String department) { super(employeeId, n, s, year, month, day); this.department=department; } public void getDetail() { super.getDetail(); System.out.println("Department : "+ department); } } public class OverrideDemo { public static void main(String s[]) { Manager manager=new Manager(101,"Anand Singh",20000, 2012,06,23,"Sales"); manager.getDetail(); } }

Output

C:\JavaProgram\inheritance\override>javac OverrideDemo.java

C:\JavaProgram\inheritance\override>java OverrideDemo
Employee Id : 101
Name : Anand Singh
Salary : 20000.0
hireDay : Sat Jun 23 00:00:00 IST 2012
Department : Sales

Download this example

Overridden methods cannot be less Accessible

class Employee { int employeeId; public Employee(int employeeId) { this.employeeId=employeeId; } public void getDetail() { System.out.println("EmployeeId : "+employeeId); } } class Manager extends Employee { String department; Manager(int employeeId, String department) { super(employeeId); this.department=department; } void getDetail() { super.getDetail(); } } public class OverrideLessAccessable { public static void main(String s[]) { Manager manager=new Manager(101,"Sales"); manager.getDetail(); } }

Output

C:\JavaProgram\inheritance\overrideLess>javac OverrideLessAccessable.java
OverrideLessAccessable.java:22: getDetail() in Manager cannot override getDetail () in Employee; attempting to assign weaker access privileges; was public
void getDetail()
^
1 error

Download this example

super class consturctor can not be overriden
class Employee { int employeeId; public Employee() { employeeId=101; System.out.println("Employee class constructor"); } public void getDetail() { System.out.println("EmployeeId : "+employeeId); } } class Manager extends Employee { String department; Employee() { } Manager(int employeeId, String department) { this.department=department; } public void getDetail() { super.getDetail(); } } public class OverrideConstructor { public static void main(String s[]) { Manager manager=new Manager(101,"Sales"); manager.getDetail(); } }

Output

C:\JavaProgram\inheritance\overrideConstructor>javac OverrideConstructor.java
OverrideConstructor.java:18: invalid method declaration; return type required
Employee()
^
1 error

Download this example

final method can not be overriden
class Employee { int employeeId; public Employee() { employeeId=101; System.out.println("Employee class constructor"); } public final void getDetail() { System.out.println("EmployeeId : "+employeeId); } } class Manager extends Employee { String department; Manager(int employeeId, String department) { this.department=department; } public void getDetail() { super.getDetail(); } } public class OverrideFinal { public static void main(String s[]) { Manager manager=new Manager(101,"Sales"); manager.getDetail(); } }

Output

C:\JavaProgram\inheritance\overrideFinal>javac OverrideFinal.java
OverrideFinal.java:22: getDetail() in Manager cannot override getDetail() in Emp
loyee; overridden method is final
public void getDetail()
^
1 error

Download this example

static method can not be overriden
class Employee { int employeeId; public Employee() { employeeId=101; System.out.println("Employee class constructor"); } public static void getDetail() { System.out.println("Display the Employee Detail: "); } } class Manager extends Employee { String department; Manager(int employeeId, String department) { this.department=department; } public void getDetail() { super.getDetail(); } } public class OverrideStatic { public static void main(String s[]) { Manager manager=new Manager(101,"Sales"); manager.getDetail(); } }

Output

C:\JavaProgram\inheritance\overrideStatic>javac OverrideStatic.java
OverrideStatic.java:22: getDetail() in Manager cannot override getDetail() in Em
ployee; overridden method is static
public void getDetail()
^
1 error

Download this example

 



 

Latest Tutorial


Most Visited Tutorial