Read Also: Method Overloading vs Method Overriding
10 Rules for Method Overriding in Java
1. In java, only inherited methods can be overridden. This is because method overriding happens only when an instance method of the parent class is re-implemented in the child class with the same signature. For example:class Parent
{
int a = 15, b = 30;
void display()
{
int c = a+b;
System.out.println("Sum = "+c);
}
}
public class Child extends Parent
{
void display()
{
int d = a*b;
System.out.println("Product = " + d);
}
public static void main(String args[])
{
Child child = new Child();
child.display();
}
}
Output:
Product = 450
In the above example, the display() method of the parent class is re-implemented in the child class with the same name and signature. Therefore, the display()method of the parent class is overridden by the display() method of the child class.
2. final and static methods cannot be overridden in java. This is because final methods can’t be re-implemented in the sub-class of a super-class. Also, only instance methods can be overridden.
class Parent
{
int a = 15, b = 30;
final void display()
{
int c = a+b;
System.out.println("Sum = "+c);
}
}
class Child extends Parent
{
void display()
{
super.display();
int d = a*b;
System.out.println("Product = " + d);
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child ob = new Child();
ob.display();
}
}
Output:
/JavaHungry.java:13: error: display() in Child cannot override display() in Parent
void display()
^
overridden method is final
1 error
In this example, the final method display() is re-implemented in child class which is not allowed in java. Therefore the compiler will throw an error.
3. The overriding method must have the same argument list. If there is any change in the argument list of the overriding method then it is called method overloading and not method overriding. For example:
class Parent
{
void display()
{
System.out.println("Display method with no arguments");
}
}
class Child extends Parent
{
void display(int value)
{
System.out.println("Display method with one argument: "+value);
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child ob = new Child();
ob.display();
ob.display(1);
}
}
Output:
Display method with no arguments
Display method with one argument: 1
As the display() method of child class has one argument whereas the display() method of parent class has no arguments, this is called method overloading and not method overriding.
4. The overriding method must have the same return type (or subtype) as that of the overridden method. If the return type( or subtype) of the overriding method is not the same as that of the overridden method, the program will not compile successfully. For example:
class Parent
{
int a = 15;
int display()
{
int b = a*a;
return b;
}
}
class Child extends Parent
{
float display()
{
float d = a/2;
return d;
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child ob = new Child();
float num = ob.display();
System.out.println(num);
}
}
This code will not compile successfully.
Output:
/JavaHungry.java:13: error: display() in Child cannot override display() in Parent
float display()
^
return type float is not compatible with int
1 error
5. In Java, the overriding method must not have a more restrictive access modifier. It means that the overriding method must have equal or lesser restrictive access. For better understanding, have a look over the following rules:
a. If the overridden method has default access then the overriding method must be the default, protected or public.
b. If the overridden method is protected then the overriding method must be public or protected.
c. If the overridden method is public then the overriding method must be public.
class Parent
{
public void display()
{
System.out.println("Inside Parent Class.");
}
}
class Child extends Parent
{
protected void display()
{
System.out.println("Inside Child Class");
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child obj = new Child();
obj.display();
}
}
Output:
/JavaHungry.java:11: error: display() in Child cannot override display() in Parent
protected void display()
^
attempting to assign weaker access privileges; was public
1 error
This type of overriding is not allowed in Java as the overriding method has more restrictive access than the overridden method.
6. The overriding method must not throw new or broader checked exceptions. It means if the overriding method throws a narrower exception or unchecked exception then the function overriding becomes legal.
import java.io.*;
class Parent
{
public void display() throws IOException
{
System.out.println("Inside Parent Class.");
}
}
class Child extends Parent
{
public void display() throws FileNotFoundException
{
System.out.println("Inside Child Class");
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child obj = new Child();
try
{
obj.display();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Output:
Inside Child Class
This is a legal overriding in Java as the overriding method throws FileNotFoundException which is a subclass of IOException.
7. The overridden method can be called from a subclass using the super keyword.
class Parent
{
public void display()
{
System.out.println("Inside Parent Class");
}
}
class Child extends Parent
{
public void display()
{
super.display();
System.out.println("Inside Child Class");
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child c = new Child();
c.display();
}
}
Output:
Inside Parent Class
Inside Child Class
8. Overriding of constructors are not allowed. This is because constructors are not methods and constructors of sub-class and super-class can’t be the same.
9. Abstract methods must be overridden by the first non-abstract(concrete) class.
abstract class Parent
{
abstract void display();
}
class Child extends Parent
{
public void display()
{
System.out.println("Inside Child Class");
}
}
public class JavaHungry
{
public static void main(String args[])
{
Child c = new Child();
c.display();
}
}
Output:
Inside Child Class
10. synchronized and strictfp modifier does not have any effect on the rules of method overriding.
Rules for Method Overloading in Java
1. In Java, method overloading can only be achieved if and only if two or more methods share the same method name but have different method signatures. If more than one method has the same name as well as signature within the same class, the program would not compile successfully. For example:class Overload
{
void disp()
{
System.out.println("No argument");
}
void disp(int x)
{
System.out.println(x);
}
void disp(int x)
{
System.out.println("Overloaded Method");
}
}
public class JavaHungry
{
public static void main(String a[])
{
Overload object = new Overload();
object.disp();
object.disp(1);
object.disp(2);
}
}
Output:
/JavaHungry.java:12: error: method disp(int) is already defined in class Overload
void disp(int x)
^
1 error
2. Method return type has no contribution in method overloading. If our program consists of more than one method with the same name, same method signature but different return types, the compiler will consider it as the duplicate declarations for the same method. Hence, the program will not compile successfully. For example:
class Overload
{
int calc(int x)
{
int y = x*x;
return y;
}
float calc(int x)
{
float y = x/2;
return y;
}
}
public class JavaHungry
{
public static void main(String args[])
{
Overload object = new Overload();
int a = object.calc(1);
float b = object.calc(3);
System.out.println(a);
System.out.println(b);
}
}
Output:
/JavaHungry.java:9: error: method calc(int) is already defined in class Overload
float calc(int x)
^
1 error
That's all for today, please mention in comments in case you have any questions related to rules for method overriding and method overloading in java with code examples.
0 Comments