• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Method Overriding In Java With Simple Examples

Method Overriding In Java With Simple Examples

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What is Method Overriding?
  • Advantages of Method overriding
  • Rules Of Method Overriding

In our previous post, we have posted about method overloading. In this post, we are going to discuss what is method overriding, rules of method overriding, and also some sample example program to understand this important concept of method overloading using Java programming language.

What is Method Overriding?

Declaring a similar method in the subclass, which is already present in the parent class is known as method overriding. We are using method overriding because it allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. That’s why the parent class method is called the overridden method, and the child class method is called the overriding method.

Let’s take an example to understand the method overriding in Java easily, suppose there is a parent class called an animal, and animal class has some method like run, walk, eat, and sleep with the implementation. There is a subclass called human, which extends the parent animal class, but the way humans are eating, walk, run all are different from the animal. So in the human class, you want to give the implementation for human methods like run, walk, eat & sleep.

Method overriding In Java With Explanation
Method overriding In Java With Explanation

So the user wants to do method overriding because he wants to give his implementation in the child class. So that when the user calls the child class methods, it operates like a human.

package com.SoftwareTestingo.JavaBasics;

class MethodOverridingEx1
{
   public void speed()
   {
      System.out.println("Parent Speed method Executed");
   }
}
public class MethodOverriding extends MethodOverloadingEx1
{
   //This Method Becomes Override the Parent Method
   public void speed()
   {
      System.out.println("Child Speed Method executed");
   }
   public static void main(String[] args) 
   {
      MethodOverriding obj=new MethodOverriding();
      obj.speed();
   }
}

Output:

Child Speed Method executed

Advantages of Method overriding

One of the significant benefits of method overriding is that when a user wants to give his specific implementation to the inherited method without doing any changes to the parent class methods.

Method overriding is helpful when there are several child class and one parent class is there, and all extend the parent class and use the parent class methods. So those classes want to use the parent class methods as it is, they can use those, and those want different implementation as per their requirement they can implement in child class so that others who are using the same method, those are not affected by this changes.

Method overriding is an example of runtime polymorphism when a parent class object reference to the child class object, and if you want to call an overridden method is determined during runtime. Because when you call a method, which method (parent or child class method) is going to call is determined according to the type of the object. SO, the process in which call to an overridden method is resolved and the process is called dynamic method dispatch.

package com.SoftwareTestingo.JavaBasics;

class Vehicle
{
   public void speed()
   {
      System.out.println("Speed Method of Vehicle Class Executed");
   }
}
class Bike extends Vehicle
{
   public void speed()
   {
      System.out.println("Speed Method Of Bike Class Executed");
   }
}
public class MethodDispatch extends Vehicle
{
   public static void main(String[] args) 
   {
      Vehicle obj=new Vehicle();
      Vehicle obj1=new Bike();
      
      obj.speed();
      obj1.speed();

   }
}

Output:

Speed Method of Vehicle Class Executed
Speed Method Of Bike Class Executed

Note: When the parent class reference refers to the child class object, then the method of child class is executed, and that is called dynamic method dispatch or runtime polymorphism.

Rules Of Method Overriding

  • Argument list: The data type of arguments and Sequence of both parent and child class should be exactly matching.
  • Overriding and Access-Modifiers: The scope of the child class method (Overriding method) can be increased, but you can not decrease the existing scope of parent class methods (Overridden method). Suppose the scope of a method in the parent class is protected than in subclass you can change the scope to the public, but you can’t change the access modifiers to private. If you change the access modifiers to private, then the Java JVM compiler will generate a compile-time error.
package com.SoftwareTestingo.JavaBasics;

class ParentCL
{
   public void display()
   {
      System.out.println("Parent Display Method Executed");
   }
}
class ChildCL extends ParentCL
{
   private void display()
   {
      System.out.println("Child Display Method Executed");
   }
}
public class MethodOverriding_Scope 
{

   public static void main(String[] args) 
   {
      // TODO Auto-generated method stub
   }
}

In the above example you can see in the child class we are trying to decrease the scope of the method public to private that’s why we are getting the below error:

Method Scope Error
Method Scope Error
  • Final Methods cannot be overridden: If you don’t want other classes to override your method, then you can declare the method with the final keyword so that no one can override your method. If anyone tries to override then he will get an error.
  • A static method can not be overridden (Method Hiding): When you define a static method in the base class and with the same method signature if you declare another static method with the same as the base class signature in the child class, that is called method hiding in Java.
package com.SoftwareTestingo.JavaBasics;

class bus
{
   public static void driver()
   {
      System.out.println("Driver Method Of Bus Class Executed");
   }
   public void conductor()
   {
      System.out.println("Conductor Method Of Bus Class Got Executed");
   }
}
class Volvobus extends bus
{
   //This Method Hides
   public static void driver()
   {
      System.out.println("Driver Method Of Volvobus Class Executed");
   }
   public void conductor()
   {
      System.out.println("Conductor Method Of Volvobus Class Got Executed");
   }
}
public class Method_Hiding 
{
   public static void main(String[] args) 
   {
      bus pobj=new bus();
      bus cobj=new Volvobus();
      
      System.out.println("+++++++++ Method Hiding +++++++++");
      System.out.println();
      pobj.driver(); //This will call the parent method
      cobj.driver(); //This will call the parent method
      System.out.println();
      System.out.println("+++++++++ Method Overriding +++++++++");
      System.out.println();
      pobj.conductor(); //This will call the parent method
      cobj.conductor(); //This will call the child method
   }
}

Output:

+++++++++ Method Hiding +++++++++

Driver Method Of Bus Class Executed
Driver Method Of Bus Class Executed

+++++++++ Method Overriding +++++++++

Conductor Method Of Bus Class Got Executed
Conductor Method Of Volvobus Class Got Executed
  • Private methods can not be overridden: Private methods can not be overridden because the scope of the private methods is within the class, so outside of the class, they cannot extend.
  • Invoking Parent Class method from child class: We Can call the parent class method from child class using the super keyword.
  • Overriding and constructor: We Can not override constructor in the child class, because the parent class constructor never extended in child class. As per the constructor rule, the constructor name should be the same as the class name. But from child class, we can call the parent class constructor by using the super keyword.
  • Abstract Method: If a class is extending an abstract class or implementing an interface, then all methods should be implemented in child class; otherwise, you will get a compile-time error.

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers