The IoT Academy Blog

What is Overriding in Java?

  • Written By  

  • Published on February 3rd, 2023

Table of Contents [show]

 

Introduction

Overriding in Java is when a child class has its method execution for the method already present in the parent class. Technically, overriding is a process that needs a subclass or child class to provide a mixture of method implementations, that are already provided by one of its superclasses or parent classes, in any object-oriented programming language. When a method in a subclass has a similar name and signature as in its super-class, the subclass is developed from the super-class. One of the ways that Java handles Run Time Polymorphism is by method overriding. The object that is used to activate a method specifies the variant of the procedure that is implemented. If it executes a method with an object from a parent class, the parent class's version will be used. But if the method is activated with an object from a subclass, the child class's version will be processed.

Why Is Overriding in Java Useful?

As earlier noted, overridden methods permit Java to accept polymorphism at runtime. Overridden methods are also another way Java adopts polymorphism in one application, many methods aspect. The most influential thing that Object-Oriented Programming brings to code reuse and robustness is dynamic process execution. The capability to use existing code libraries to call methods on new class instances without recompiling while maintaining a clean abstract interface is an extremely strong weapon. Overridden methods permit one to call methods from any object of a derived class without specifying the form of the modified superclass.

 

 

Our Learners Also Read: What is Overloading in Java?

When Should You Use Overriding in Java?

Recognizing the parent and child classes in a hierarchy that provides from lower to higher productivity is part of the secret to effectively using polymorphism. The parent class, when used correctly, includes all the factors that a child class would access instantly. It also specifies which methods the child class must implement separately. This provides the child class with the ability to share its methods while maintaining a standard interface. A parent class will specify the general form of the methods that are used by all of its child classes by combining inheritance and overridden methods. 

Rules For Method Overriding:

 

1.Overriding and Access-Modifiers: The access modifier for an overriding method can permit more, but not less, access than the overridden method. For instance, a protected model method in the superclass can be made public, but not private, in the subclass. Accomplishing so will cause a compile-time error.

 

2.Final Methods can not be Overridden: If we don't like a method to be overridden, we state it as final.

 

3.Static Methods can not be Overridden(Method Overriding vs Method Hiding): When you specify a static method with the same signature as a static method in the base class, it is called method hiding.

 

4.Private Methods can not be Overridden: Private methods cannot be overridden as they are connected during compile time. Thus we cannot even override private methods in a subclass.

 

5.The Overriding method must have the same return type (or subtype): From Java 5.0 along, it is likely to have distinct return types for an overriding method in the child class, but the child return type should be a sub-type of the parent return type. This phenomenon is called the covariant return type.

 

6.Invoking Overridden Method from sub-class: We can invoke the parent class method in the overriding method by the super keyword.

 

7.Overriding and Constructor: The parent and child class can never have a constructor with the same name thus we can not override the constructor(The constructor name must always be the same as the Class name).

 

Conclusion

In Javamethod overriding happens when a subclass (child class) has the similar process as the parent class. In different words, method overriding happens when a subclass provides a special implementation of a method claimed by one of its parent classes.

 

full stack java developer course

 

 

About The Author:

logo

Digital Marketing Course

₹ 9,999/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 29,999/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot
1whatsapp