Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Overriding in Java

Last updated by Swaminathan Iyer on Sep 25, 2024 at 11:00 PM | Reading time: 8 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Overriding in Java
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

If you’re a software engineer or developer preparing for a tech interview, and Java is your preferred programming language, brushing up on the basic concepts will help you solve problems with more ease. We know that Java is a high-level object-oriented programming language. It offers many object-oriented properties. Overriding is one of the important features in Java. In this article, we’ll review what overriding is and how it works:

  • What is overriding in Java?
  • Overriding vs. overloading in Java
  • Rules for Java method overriding
  • Java method overriding example
  • Method overriding and dynamic method dispatch
  • Super keyword in method overriding
  • Advantages of method overriding in Java
  • FAQs on method overriding in Java

What Is Overriding in Java?

In Java, declaring a method in a subclass that is already present in a parent class is called method overriding. The main purpose of having overridden methods in the child is having a different implementation for a method that is present in the parent class. 

  • Overriding method: The method in child class that is the same as a method in the parent class. 
  • Overridden method: The method in the parent class that is overridden by the child class method. 

Advantages of Method Overriding

  • Method overriding helps in writing a generic code based on the parent class. 
  • It provides multiple implementations of the same method and can be used to invoke parent class overridden methods using super keyword. 
  • It defines what behavior a class can have. 

Overriding vs. Overloading in Java

Method overriding is used to create a specific implementation of a method that is already present in the parent class. Method overriding occurs in two classes, where one class inherits from another class. In overriding, the method return type and function parameters of overriding and overridden methods must be the same. 

Method overloading allows more than one method in a class to have the same name with different parameters. Basically, we can create multiple methods with the same name in the same class. However, each method works differently. It makes the program easier to read (readable). 

Here’s a summary of the key differences between overriding and overloading in Java:

Rules for Java Method Overriding

  • Method name, its function parameters, and the return type of the overriding method and overridden method must be the same. 
  • The access modifier of an overriding method cannot be more stringent than the access modifier of an overridden method. 
  • Static, Private, and Final methods cannot be overridden because of their scope.
  • If the class extends an abstract class or implements an interface, it must override all of the abstract methods, provided the class itself is not abstract. 

Java Method Overriding Example

In the example given below, b is an object of class Topic, and after calling the b.say() method, it will execute the method say() in the Topic class. Here, class Topic is inherited from the class Physics, and both classes have method say(). But the method say() of class Topic overrides the method say() of class Physics; hence after calling b.say(), it executes the code inside the say() method of Topic class. 

In the example above, the program will be compiled because the class Topic has its method say. Therefore, at the time of execution, it carries on a specific path to the given object.

Code:

// Class physics

class Physics {

    // method say which is overridden method here

   public void say() {

      System.out.println("This is class Physics");

   }

}

// Class Topic

class Topic extends Physics {

    // method say which is overriding method here

   public void say() {

      System.out.println("This is class Topics");

   }

}

class Main {

   public static void main(String args[]) {

      Physics a = new Physics(); // Physics reference and object

      Physics b = new Topic(); // Physics reference but Topic object

      a.say(); // runs the method in Physics class

      b.say(); // runs the method in Topic class

   }

}

Output: 

This is class Physics

This is class Topics

Method Overriding and Dynamic Method Dispatch
In method overriding, during method call in the main function, which method should be executed (child class or parent class) is determined at runtime by the object type. The process where the call to the overridden method is resolved at runtime is called dynamic method dispatch.

Super Keyword in Method Overriding

The super keyword in Java is used for calling the parent class method or constructor. For example, let’s consider a method named newMethod() in the parent class, then super.newMethod() can be used to call the newMethod() method of parent class. super() can be used to call the constructor of the parent class. 

So, we can call the overridden method by using the super keyword. 

Code:

// Class physics

class Physics {

    // method say which is overridden method here

   public void say() {

      System.out.println("This is class Physics");

   }

}

// Class Topic

class Topic extends Physics {

    // method say which is overriding method here

   public void say() {

       // this will call say method of Physics Class

       super.say();

      System.out.println("This is class Topics");

   }

}

class Main {

   public static void main(String args[]) {

      Physics a = new Physics(); // Physics reference and object

      Physics b = new Topic(); // Physics reference but Topic object


      a.say(); // runs the method in Physics class

      b.say(); // runs the method in Topic class

   }

}

Output:

This is class Physics

This is class Physics

This is class Topics

FAQs on Method Overriding in Java 

Question 1: How to prevent overriding a method without using the final modifier in Java?

There are some unusual ways to prevent method overriding in Java. Though the final modifier is only for that purpose, we can use the private keyword to prevent method overriding. In order to override a method, the class must be extensible. If we make the constructor of the parent class private, it is impossible to extend that class because its constructor will not be accessible in the subclass, which the sub-class constructor automatically invokes. Hence, it is not possible to override any method from that class. 

Question 2: Can we override the constructor in Java?

No. We cannot override constructors in Java because they are not inherited. We can overload constructs, but we cannot override them. Overriding always happens in the child class, and since constructors are not inherited, and their name is always the same as the class name, it is not possible to override them in Java.

Question 3: Can we override the static method?

In the case of static methods, there won't be any runtime polymorphism. So, static methods can not be overridden. However, if a child class has a defined static method with a similar signature as the static method of a parent class, then the method in the parent class will be hidden by the method in the child class. 

Recommended Reading

Check out our learn page for more articles on Java:

To learn more tech interview questions and problems, check out the following pages: Interview Questions, Problems.

Ace Your Next Tech Interview With Interview Kickstart!

If you’re looking for guidance and help to nail your next technical interview, sign up for our free webinar

As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land their dream jobs at Google, Facebook, Apple, Netflix, Amazon, and other Tier-1 tech companies.

Join our webinar to learn more!

---------

Article contributed Omkar Deshmukh

Last updated on: 
November 20, 2024
Author

Swaminathan Iyer

Product @ Interview Kickstart | Ex Media.net | Business Management - XLRI Jamshedpur. Loves building things and burning pizzas!

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
First Name Required*
Last Name Required*
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

Overriding in Java

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image