– All the methods that parent class has are by default available to child class because of inheritance. If the child class method isn’t satisfied with its inherited method it is free to redefine based on its requirement. This is known as “OVERRIDING”.
Let us take an example to understand overriding better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | class Parent { public void property() { System.out.println("Cash + Land + Gold"); } public void marry() { System.out.println("Subbalaxmi"); } } class Child extends Parent { public void marry() { System.out.println("Samantha/Sonam/Emma"); } } class Test { public static void main (String[] args) { Parent p = new Parent(); p.marry(); //parent method: Subbalaxmi Child c = new Child(); c.marry(); //Child method : Samantha/Sonam/Emma Parent p1 = new Child(); p1.marry(); //Child method : Samantha/Sonam/Emma } } |
Things to remember regarding Overriding in Java
(1) In overriding method names and argument types must be matched i.e., method signatures must be same.
The return types must be same in overriding upto java 1.4V but from 1.5v co-variant return types are allowed, that means child class method return type need not be same as parent class return type, its child types are also allowed.
Eg:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Parent { public double m1() { return null; } } class Child extends Parent { public int m1() { return null; } } class Test { public static void main(String[] args) { Child c = new Child(); c.m1(); } } //This is valid. |
Note that covariant return types are only applicable for object types but not for primitive types.
Eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Parent { public double m1() { return 11.5; } } class Child extends Parent { public int m1() { return 11; } } class Test { public static void main(String[] args) { Child c = new Child(); c.m1(); } } //This is invalid. /*Output would be : error: m1() in Child cannot override m1() in Parent public int m1() ^ return type int is not compatible with double */ |
(2) Parent class private methods are not visible to child classes. Hence overriding concept isn’t applicable for private methods. But based on our requirement, we can define same parent class private method in child class, its valid but its not overriding.
Eg:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Parent { private void m1() { //any code } } class Child extends Parent { private void m1(); } // This is valid. |
(3) We cannot override parent class final methods.
Eg:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | class Parent { public void property() { System.out.println("Cash + Land + Gold"); } public final void marry() { System.out.println("Subbalaxmi"); } } class Child extends Parent { public void marry() { System.out.println("Samantha/Sonam/Emma"); } } class Test { public static void main (String[] args) { Parent p = new Parent(); p.marry(); //parent method: Subbalaxmi Child c = new Child(); c.marry(); //Child method : Samantha/Sonam/Emma } } /* OverridingDemo5.java:15: error: marry() in Child cannot override marry() in Pare nt public void marry() ^ overridden method is final */ |
(4)We should override parent class abstract method in child class to provide implementation.
We can override parent class non-abstract method as abstract.
Eg:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Parent { void m1() { //any code } } class Child extends Parent { abstract void m1(); } // This is valid. |
“synchronized”, “native”, “strictfp”, “abstract” modifiers won’t keep any restriction on overriding.
(5)While overriding, we can increase scope of access-modifier but we can’t reduce.
Eg:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Parent { public void m1() { } } class Child extends Parent { protected void m1(); } //This is invalid /* OverridingDemo7.java:11: error: m1() in Child cannot override m1() in Parent protected void m1(); ^ attempting to assign weaker access privileges; was public 1 error */ |
(6) The overriding method can throw any unchecked exceptions regardless of whether the overridden method declares exception.
The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method.
The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” that doesn’t mean that the overriding subclass exception takes same risks.i.e “an overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares”.
(7)Overriding concept is applicable only for methods but not for variables.
Variable resolution is always taken care by compiler based on “reference type” irrespective of the variables being static or non-static.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Parent { int x =777; } class Child extends Parent { int x =888; } class Test { public static void main(String []args) { Parent p = new Parent(); //777 System.out.println(p.x); Child c= new Child(); System.out.println(c.x); //888 Parent p1= new Child(); System.out.println(c.x); //888 } } |
(8)We cannot override static method as non-static, vice-versa
Eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class P { public static void m1() { System.out.println("Hello!"); } } class C extends P { public void m1() { System.out.println("Hello!"); } } //Not valid |
I hope I covered everything about Overriding in Java..
Please comment below if you have any doubts..
[…] Overloading , Overriding […]
Hi Harsha, nice covering of all concepts related to overriding.
But my doubt is, we know all these rules like we should not reduce the scope of parent class method in child class when we override the method and child class overriding method shouldn’t throw checked exceptions that are higher than the checked exceptions thrown by the parent class. Incase of parent class overridden method without any checked exception list, the child class overridden method has no choice to throw any checked exceptions even required also. Because of this scenario, the child class has no freedom to override parent class method to match/reach/achieve its requirement.
I’m waiting for ur reply
once again thanks for ur explanation………..
While overriding, we can increase scope of access-modifier but we can’t reduce. but Why?
Hi,
In 7th point. I believe you wanted to write:
System.out.println(p1.x); //777
Thanks for pointing out typo !