Whatever methods the parent class has by default are available to the child class through inheritance. If the child class is not satisfied with the method implementation available in the parent class then the child class is allowed to redefine that method based on its requirement. this process is known as method overriding.
The parent class method which is overriding is called the overridden method and the child class method which is overriding parent class implementation is called the overriding method.
class Parent {
public void greeting(){
System.out.println("Greeting from parent");
}
}
class Child extends Parent{
@Override
public void greeting() {
System.out.println("Greeting from child");
}
}
In the overriding method resolution always take care of by JVM based on the runtime object, hence overriding is also considered as runtime polymorphism or dynamic polymorphism late binding.
class Parent {
public void greeting(){
System.out.println("Greeting from parent");
}
}
class Child extends Parent{
@Override
public void greeting() {
System.out.println("Greeting from child");
}
}
class TheCodeData {
public static void main(String[] args) {
Parent parent = new Parent();
parent.greeting();
Child child = new Child();
child.greeting();
Parent parentt = new Child();
parentt.greeting();
}
}
Rules For Method Overriding
- In the overriding method names must be the same that is method signature(name and arguments type) must be the same.
- In overriding return type must be the same but this rule is applicable until Java1.4v, from Java 1.5v onwards we can take a co-variant return type. According to this child class method return type need not be the same as the parent class method return type.
package MethodOverriding;
public class Parent {
public Object method(){
return null;
}
}
class Child extends Parent{
public String method(){
return null;
}
}
Folowing co-varient return type is acceptable
Parent Object -> Object/String/StringBuffer Child
Parent Number -> Number/Integer Child
Co-varient return type concept is only applicable for object types, not for primitive types.
- Parent class private method is not available to the child class hence overriding concept does not apply to the private method. Based on our requirement we can declare the same method in the child class but it is not method overriding.
public class Parent {
private void method(){
System.out.println("Parent class method");
}
}
class Child extends Parent{
private void method(){
System.out.println("Child class method");
}
}
- We cannot override the parent class final method in the child class, If we try to do so then we will get compile time error-“‘method()’ cannot override ‘method()’ in ‘MethodOverriding.Parent’; overridden method is final”.
package MethodOverriding;
public class Parent {
public final void method(){
System.out.println("Parent class method");
}
}
class Child extends Parent{
public void method(){
System.out.println("Child class method");
}
}
- We should override the parent class abstract method in the child class to provide the implementation.
package MethodOverriding;
abstract class Parent {
public abstract void method();
}
class Child extends Parent{
public void method(){
System.out.println("Child class method");
}
}
- We can override the nonabstract method as abstract. The main advantage of this is we can stop the availability of parent class method to next level child class.
package MethodOverriding;
public class Parent {
public void method(){
System.out.println("Parent class method");
}
}
abstract class Child extends Parent{
public abstract void method();
}
Parent method modifier and Child method modifier rule
Parent method modifier | child method modifier | valid/invalid |
---|---|---|
final | non-final/final | Invalid |
non-final | final | Valid |
abstract | non-abstract | Valid |
synchronized | non-synchronized | Valid |
native | non-native | Valid |
strictfp | non-strictfp | Valid |
In overriding there is no restriction on following modifiers –
- synchronized
- native
- strictfp
Rules for Modifiers in Overriding
- While overriding we can not reduce the scope of the access modifier but we can increase the scope.
package MethodOverriding;
public class Parent {
public static void method(){
System.out.println("Parent method");
}
}
class Child extends Parent{
public static void method(int x){
System.out.println("Child method");
}
}
class TheCodeData{
public static void main(String[] args) {
Parent p = new Parent();
p.method();
Child c = new Child();
c.method();
}
}
- We cannot override a static method as a nonstatic method otherwise we will get compile time error.
- We cannot override a nonstatic method as static. otherwise, we will get compile time error.
- If both parent and child class methods are static then we will not get any compile time error but this is not overriding it is method hiding.
Method Hiding
All rules of method hiding are exactly same as overriding except the following diffrences.
Method Hiding | Method Overriding |
---|---|
Both methods should be static | Both method should be non-static |
The compiler is responsible for method resolution based on reference type | JVM is responsible for method resolution based on runtime objects. |
It is also known as compile-time polymorphism, static polymorphism, or early binding | It is also known as runtime polymorphism, dynamic polymorphism, or late polymorphism |
Overriding with respect to var-args method
We can override the var-arg method with another var-args method only if we are trying to override with the normal method, then it will become overloading but not overriding.
package MethodOverriding;
public class Parent {
public void method(int... x){
System.out.println("Parent method");
}
}
class Child extends Parent{
public void method(int x){
System.out.println("Child method");
}
}
class TheCodeData{
public static void main(String[] args) {
Parent p = new Parent();
p.method(45);
Child c = new Child();
c.method(67);
Parent p1 = new Child();
p1.method(56);
}
}
In the above Java program if we replace the child method with the var-arg method then it will become overloaded.
Overriding with respect to variables
Variables resolution always takes care of by the compiler based on reference type irrespective of whether the variable is static or nonstatic (The overriding concept is only applicable for methods not for variables).
package MethodOverriding;
public class Parent {
int var = 111;
}
class Child extends Parent{
int var = 222;
}
class TheCodeData{
public static void main(String[] args) {
Parent p = new Parent();
System.out.println(p.var);
Child C = new Child();
System.out.println(C.var);
Parent p1 = new Child();
System.out.println(p1.var);
}
}
Similar Java Tutorials
- Bitwise operator in Java
- Operators in Java
- Public static void main
- Command line argument in Java
- Java coding standards
- Control Flow Statements in Java
- If – else in Java
- Switch statements in Java
- For loop in Java
- While loop in Java
- Do while loop in Java
- Import statement in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Static modifier in Java
- Native modifier in Java
- Transient keyword in Java
- Volatile keyword in Java
- Adapter Class in Java
- Method signature in Java
- Method overloading in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- Data Hiding in Java
- Interface vs abstract class
3 thoughts on “Method Overriding in Java”