Method Overriding in Java

By | July 26, 2020

Method Overriding in Java is to provide the different logic to the method in the subclass which is inherited from the superclass.

That means the method which presents in the superclass is also present in the subclass or child class.

But the logic or implementation of the method is completely different compare to the logic of method in the parent or superclass.

More if give you a more technical definition, then Method Overriding is the process of providing the subclass specific method implementation or method definition for an inherited method.

Note: Method Overriding is only possible in the case of Inheritance.

Method Overriding in JavaHence the prerequisite to override any method is that method with the same name must be present in parent class also.

Usage of Method Overriding

  1. Method Overriding in java help to achieve Runtime Polymorphism.
  2. It provides us the flexibility to use our own business logic or implementation based on the requirement.

Method Overriding in java help to achieve Runtime Polymorphism

To understand the above sentence first we would like to explain briefly about Runtime Polymorphism.  So, it provides the ability to methods to behave differently based on the Object it is acting upon, that means method with the same name but only the logic is getting changes as per the Object.

Consider an example, I have created a one Superclass name Engineer having a method work() inside that class. There there are two subclasses such as Software Engineer and Mechanical Engineer, which extends or inherits the superclass.

Once these child classes inherit the superclass the method work() will also get inherited to subclasses.

Since the work performed by both the engineers are completely different from each other, So the logic or definition provides to work() method in SoftwareEngineer class always be different than Mechanical Engineer.

Hence you can see we are using the same method work() in both the classes but the logic we provide is completely different. This is how Method Overriding helps to achieve Runtime Polymorphism in Java.

Rules of Method Overriding

  1. Method name must be the same as declared in the superclass.
  2. Method return type must be the same as declared in the superclass.
  3. Method signature must be the same as declared in the superclass.
  4. Method logic or implementation must be different from the superclass method.

Note: Same Method Signature in subclass refers to any of the following points.

  • The number of parameters method accept when it gets invoked must be the same as in the superclass.
  •  Parameters must be passed in the same sequence in the subclass.
  • The data type of the parameter must be the same as in the superclass.

Difference between Method Overloading and Method Overriding.

Method Overloading Method Overriding

Two or more methods can have the same name but a different signature.

Method name along with its signature should be the same in child classes as in superclass.

Method Overloading can be done in the same class.

Method Overriding is only possible in the sub or child class of the superclass.
Inheritance is not required to achieve method overloading.  Without the Inheritance method overriding is not possible.
The return type of overloaded method can be different. The return type for the overridden method should always be the same.
It helps to achieve the Compile Time Polymorphism in Java. Runtime Polymorphism can be achieved with method overriding.
  It is not recommended from the Performance aspect.
Memory point of view it is not recommended. It is suitable to use if your priorities are to save some memory space.

Important Terms frequently used in Java

UP Casting

A superclass reference referring to or storing the address of any of its subclass objects then it is called Up Casting that is Object reference is of superclass but the Object is from its subclass.

Characteristics of Up Casting

  1. In the case of Up Casting, using superclass reference we cannot call the subclass specific method and variable.
  2. In Up Casting, using the superclass reference when we invoke or call an overridden method then the logic or implementation is executed from the subclass.

Down Casting

A subclass or derived class type is referring to the parent class Object then it is a case of Down Casting.

Important Point About Down Casting

  • We do downcast to access the specific behavior of an Object of subclass Type. 
  • If downcasting is logically wrong then we get ClassCastException at RunTime in Java.

Instance Block

Instance block is also called as Object Block, it is defined in the class with {} braces. The execution of this block happens at the time of Object Creation.

Important Points of Instance Block

  1. It is automatically executed by JVM at the time of Object Creation.
  2. Instance block can be executed multiple times, that is the number of times instance block executed depends on the number of Objects.
  3. We can have multiple instance block in class. 

Data Member

A data member is one of the contents of class along with the constructor and methods. 

Data Members are of two types.

  1. Variables: The data members whose value is changed even after declaration.
  2. Constant: Once the constant data member declared with some value, then it not going to be changed anywhere in the code. We use final keyword for Constant data members.

Important Points about data member

  • We can initialize the data member at the time of declaration or we can just declare and initialize it later.
  • Final data members work as a constant, so it should be initialized at the time of declaration only.

Good Practice for Data Members

  1.  Variable Data Members: The first letter of the word must be in LOWERCASE and the first letter of the consecutive word must be in UPPERCASE.
  2. Constant Data Members: Always use UPPERCASE for a variable which we make constant.

Thanks a lot for reading my article. It’s highly appreciated.  If you have any queries or doubt please do comment below. I will love to answer your questions or doubts.

If you have any feedback or suggestion please feel free to mail or comment it below.

Leave a Reply

Your email address will not be published. Required fields are marked *