Defining a method in subclass that has same signature as a method in superclass is known as method overriding.here signature means same method name same parameters and. Overriding also known as runtime polymorphism or dynamic polymorphism.
Lets see with diagram
In above Person class is super class and Teacher class is subclass . so here we are defining the Person class method displayDetails() in Teacher Class with same method signature as in Person class.this is what method overriding.Lets see below Example
package com.java2study;
public class Person {
private int personId = 123;
public void displayDetails(){
System.out.println("person Id:"+personId);
}
}
package com.java2study;
public class Teacher extends Person {
private String name;
public void displayDetails(){
System.out.println("teacher name:"+name);
}
public static void main(String[] args) {
Teacher t = new Teacher();
t.name = "AAAA";
t.displayDetails();
}
}
output
teacher name:AAAA
method Overriding Rules
- method signatures must be same which means method name and parameter list and return type.like below
- Return type must be same until java 1.4 version but from java 1.5 version we can take Co-variant return types which means subclass type as a Return type. for example if a parent class method has return type Number then child class return types should be Integer or float etc.. like below
- access modifier should be same or less Restrictive modifier allowed to overridden methods. for example if super class method has protected modifier then in subclass we can take modifiers as protected or public
- private is the high restrictive modifier in java. and later default and later protected and later public (private<default<protected<public).
- If super class method throws some checked exception then compulsory subclass method should throw the same checked exception or it's child class exception. otherwise we will get compile time error.like below
super class method
public void add(int x,int y){
}
subclass method
}
public void add(int x,int y){
super class method
public Number add(int x, int y) {
return x + y;
}
subclass method
public Integer add(int x,int y){
return x+y;
}
class A{
protected void print(){
System.out.println("A class instance method");
}
}
public class B extends A{
protected/public void print(){
System.out.println("B class instance method");
}
public static void main(String[] args){
B b = new B();
b.print();
}
}
Exception Handling In Overriding
package com.java2study;
public class A {
public Number add(int x, int y) throws Exception{
return x + y;
}
}
output
package com.java2study;
public class B extends A {
private String name;
public Integer add(int x,int y) throws IOException{
return x+y;
}
public static void main(String[] args) {
B b = new B();
Integer i = b.add(7, 5);
System.out.println(i);
}
}
important points about overriding
12
»private methods cannot be overridden.
»final is a keyword to prevent overriding.so final methods cannot be overridden.
»We cannot override static methods