What is Overriding in Java ?

Jiledar_Dhakad
4 min readJul 25, 2023

When a child class or subclass is not satisfied with the method implementation of its parent class or superclass which is available to the child class through inheritance. Then the child class is provided implementation to that method in its own way. This process is known as overriding. In an overriding feature, the parent class method is called overridden method and the child class method is called the Java overriding method.

Overriding in Java

Example of Overriding in Java

We can create a simple example to understand this concept. In this example, we have two classes i.e., Parent class and Child class. Parent class has a marry() method and Child class extends Parent class. But the Child class is not satisfied with the implementation of the marry() method, so the Child class is providing implementation of this method in its own way. Thus, the Child class is overriding the marry() method.

class Parent{
public void marry(){
System.out.println("Arrange Marriage with the Parent's choice");//overridden method
}
}
class Child extends Parent{
public void marry(){
System.out.println("Love/Arrange Marriage with His/Her choice");//overriding method
}
}
public class Test {
public static void main(String args[]) {
Parent p=new Parent();
p.marry();
Child c=new Child();
c.marry();
Parent p1=new Child();
p1.marry();
}
}

Output

Arrange Marriage with the Parent's choice
Love/Arrange Marriage with His/Her choice
Love/Arrange Marriage with His/Her choice

→ In the overriding method resolution always takes care of the JVM(Java Virtual Machine) based on the runtime object hence overriding is also known as runtime polymorphism or dynamic polymorphism or late binding.
→ The process of overriding method resolution is also called dynamic method dispatch.

Rules for Method Overriding in Java

  1. In the overriding method names and arguments must be the same i.e., the method signature must be the same.
    2. In method overriding used return types must be the same till Java 1.4 version but from Java 1.5 version onwards co-variant return types are allowed to use.
  2. Example of Co-Variant return in Method Overriding
class Parent {
public Object method1()
{
System.out.println("This is the method in the Parent class");
return new Object();
}
}
class Child extends Parent {
public String method1()
{
System.out.println("This is the method in the Child class");
return "";
}
}
public class Test {
public static void main(String[] args)
{
Parent p = new Parent();
p.method1();
Child c = new Child();
c.method1();
}
}

Output

This is the method in the Parent class
This is the method in the Child class

3. Private methods are not visible in the Child classes hence overriding concept is not applicable to private methods.
4. The final methods of the parent class we can’t override in the Child class.

Example of the final method in Overriding

class Parent {
public final void method1() {}
}
class Child extends Parent {
public void method1() {}
}
public class MyClass {
public static void main(String args[]) {
Parent p=new Child();
p.method1();
}
}

Output

Test.java:6: error: method1() in Child cannot override method1() in Parent
void method1() {}
^
overridden method is final
1 error

5. While method overriding we can’t reduce the scope of the access modifier.

Example of access modifier in method overriding

class Parent{
public void method1()
{
System.out.println("This is the Parent class method");
}
}
public class Child extends Parent{
protected void method1(){
System.out.println("This is the Child class method");
}
public static void main( String args[]) {
Parent p = new Child();
p.method1();
}
}

Output

Compile time error:
/Child.java:8: error: method() in Child cannot override method() in Parent
protected void method(){
^
attempting to assign weaker access privileges; was public
1 error

→ public → public protected → protected/public
→ <default> → <default>/protected/public
6. While overriding if the child class throws any checked exception compulsory the parent class method should throw the same checked exception or its parent otherwise we will get compile time error. But there are no restrictions for unchecked exceptions in method overriding.

Example of checked exception in method overriding

class Parent {
public void method1() {}
}
class Child extends Parent {
public void method1() throws Exception {}
}
public class Test {
public static void main(String args[]) {
Parent p=new Child();
p.method1();
}
}

Output

/Test.java:6: error: method1() in Child cannot override method1() in Parent
public void method1() throws Exception {}
^
overridden method does not throw Exception
1 error

Example of unchecked exception in method overriding

class Parent {
public void method1() {}
}
class Child extends Parent {
public void method1() throws ArithmeticException {}
}
public class Test {
public static void main(String args[]) {
Parent p=new Child();
p.method1();
}
}

7. We can’t override a static method as non-static and a non-static method as static.
8. A var-arg method should be overridden with the var-arg method only. If we are trying to override with the normal method then it will become overloading but not overriding.

Conclusion

In this topic, we learnt about overriding in Java and the rules of method overriding in Java.

--

--

Jiledar_Dhakad

Spring Java tutorials provides well contents to learn java, spring framework and also the architecture of spring technology for beginners and professionals.