Interview Kickstart has enabled over 21000 engineers to uplevel.
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:
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.
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:
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.
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
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.
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.
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
Attend our webinar on
"How to nail your next tech interview" and learn