MethodOverriding in Java

Method overridingis a feature in java which is used to provide specific implementation of a method in sub class that is already provided by its super class.

Runtime polymorphism is achieved by method overriding.

Rules for method overriding:1 :Method signature must be same including return type, number of method parameters, type of parameters and order of parameters.2 :The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

packagecm.ai1tutorial.java.inheritance;

classA{

publicAget(){

returnthis;

}

}

publicclassBextendsA{

publicBget(){

returnthis;

}

publicvoidprint(){

System.out.println("Inside class B");

}

publicstaticvoidmain(String[]args){

newB().get().print();

}

}

You can think of another example where a super class method has return type as List and sub class can have same method with sub type of List i.e. ArrayList as return type.3. Overriding method can not throw higher Exception than original or overridden method. means if original method throws IOException than overriding method can not throw super class of IOException e.g. Exception but it can throw any sub class of IOException or simply does not throw any Exception. This rule only applies to checked Exception in Java, overridden method can throw any unchecked Exception.4.The access level cannot be more restrictive than the overridden method’s access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected or default.5. Final method can not be overridden.6. Static method can not be overridden.A static method can be called without creating instance of class.It is loaded at compile time.While overriding is a run time polymorphism concept.7. A subclass in a different package can only override the non-final methods declared public or protected.

Instance methods can be overridden only if they are inherited by the subclass.

A method declared final cannot be overridden.

A method declared static cannot be overridden but can be re-declared.

If a method cannot be inherited, then it cannot be overridden.

A subclass within the same package as the instance’s superclass can override any superclass method that is not declared private or final.

A subclass in a different package can only override the non-final methods declared public or protected.

An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.