World's most popular travel blog for travel bloggers.

Some Rules of Method Overriding

, , No Comments

1. Same Argument List :

The argument list should be exactly the same as that of the overridden method.
package com.c4learn.inheritance;

public class InheritanceRules {

    public int calculate(int num1,int num2) {
       return num1+num2;
    }

    public static void main(String[] args) {
       baseClass b1 = new baseClass();
       int result = b1.calculate(10, 10);
       System.out.println("Result : " + result);
    }
}

class baseClass extends InheritanceRules {

    public int calculate(int num1,int num2) {
       return num1*num2;
    }
}
In the above example, method is having the same set of parameter list –
public int calculate(int num1,int num2)

2. The return type :

Return type should be the same or a subtype of the return type declared in the Original Overridden method in the Parent Class.
Consider the following method from Parent Class –
public int calculate(int num1,int num2) {
    return num1+num2;
}
then we cannot change the return type of the method in the overriden method.

3. Modification of access level :

Access level cannot be lowerd down or restricted than the overridden method’s access level.
Suppose currently method in the Parent class is having the access level public then we cannot override the method with access level as private and protected.
below is the table which summarized the which are the allowed ways of modifing the access level of overriden method –
Access Level in ParentAccess Level in ChildAllowed ?
PublicPublicAllowed
PublicPrivateNot Allowed
PublicProtectedNot Allowed
ProtectedPublicAllowed
ProtectedProtectedAllowed
ProtectedPrivateNot Allowed

Following method will throw compile time error –
package com.c4learn.inheritance;

public class InheritanceRules {

    public int calculate(int num1,int num2) {
       return num1+num2;
    }

    public static void main(String[] args) {
       baseClass b1 = new baseClass();
       int result = b1.calculate(10, 10);
       System.out.println("Result : " + result);
    }
}

class baseClass extends InheritanceRules {

    private int calculate(int num1,int num2) {
       return num1*num2;
    }
}
because in the parent class, Method has Access level public while we have made access level as private in the child class.

4. Cannot Override Final Methods :

We cannot override the final method declared in the Parent Class/Super Class. Consider the following method is written inside the Parent Class –
final public int calculate(int num1,int num2) {
    return num1+num2;
}

5. Cannot Override Static Methods but Re-declaration is possible :

Consider the following method declared inside the Parent Class –
public static int calculate(int num1,int num2) {
    return num1+num2;
}
then child class cannot override static method from parent class but it can redeclare it just by changing the method body like this –
public static int calculate(int num1,int num2) {
    return num1*num2;
}
however we have to keep the method static in the child class, we cannot make it non-static in the child class, So following method declaration inside child class will throw error –
public int calculate(int num1,int num2) { //without static
    return num1*num2;
}

6. Rule for Methods that Cannot be Inherited :

If a method cannot be inherited, then it cannot be overridden by the child class. We cannot inherite the private methods of the parent class in the child class. So private methods cannot be overriden by the childe class. However redeclaration of private method is possible in the child method –
public class InheritanceRules {

    private int calculate(int num1,int num2) {
       return num1+num2;
    }
}

class baseClass extends InheritanceRules {

    private int calculate(int num1,int num2) {
       return num1*num2;
    }
}

0 comments:

Post a Comment

Let us know your responses and feedback