24
JanExplore the Concept of Overriding in Java
Understanding Overriding in Java
Have you ever wondered how you can customize the behavior of a method in Java without changing the original code in the parent class? That’s where method overriding comes in! It allows a subclass to provide its own version of a method, making your programs more dynamic and adaptable.
In this Java Tutorial, we’ll explore the concept of Overriding in Java, how it works, and when to use it. Whether you’re new to Java or refining your skills, this guide will help you understand method overriding and how to use it effectively in your projects.
Become a certified full-stack developer with our expert-led Java Full Stack Developer Certification Training. Enroll today!
What is Overriding in Java?
- Ever wanted to change how a method works? That’s where method overriding helps you. It allows a subclass to replace a method defined in its parent class.
- In Java, overriding happens when a subclass defines a method with the same name, return type, and parameters as the one in the superclass.
- When you call the method using the object of the subclass, Java executes the overridden method in the subclass instead of the one in the parent class.
Why is Overriding Important?
- It supports polymorphism, letting you create flexible and reusable code.
- You can use the same method name but customize its behavior for specific subclasses.
- It ensures your program follows object-oriented concepts.
Read More: Polymorphism in C++ |
Key Points to Remember:
- Always use the @Override annotation to make your intent clear and avoid mistakes.
- The method signature (name, return type, and parameters) must match exactly.
- The access modifier in the overriding method can’t be more restrictive than in the parent method.
Overriding makes your code efficient and easier to maintain. Doesn’t that sound like a powerful tool to have?
When Is It Ideal to Apply Overriding in Java?
- Need to provide specific behavior: Use overriding when a subclass requires a method to perform differently than it does in the parent class.
- Follow object-oriented principles: Overriding is key to implementing polymorphism, allowing a single interface to represent different behaviors.
- Enhance flexibility: Overriding helps you adapt methods to suit your subclass without altering the parent class code.
- Work with runtime polymorphism: When you want the method to be resolved at runtime based on the object type, overriding is the ideal choice.
- Maintain code reusability: You can keep shared logic in the parent class and customize specific parts in subclasses using overriding.
Examples of Ideal Use Cases:
- Creating a custom implementation: For instance, overriding the
toString()
method in your class to provide a meaningful string representation. - Building frameworks: Frameworks often rely on overriding methods like
onCreate()
in Android orpaint()
in Swing. - Specializing behavior: Overriding methods like
equals()
andhashCode()
to compare objects meaningfully.
Overriding is a powerful tool in Java. It helps you write code that is more flexible, reusable, and aligned with object-oriented programming principles. So, isn’t it worth mastering?
Rules for Java Method Overriding
1. The Overriding and Access Modifiers
Overriding and Access Modifiers let you redefine a method in a subclass while keeping its structure from the parent class. To override a method, use the same method name, return type, and parameters. The access modifier in the overridden method should be as permissive or more permissive than the parent method.
Example
class Parent {
protected void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
@Override
public void display() { // Access modifier is more permissive
System.out.println("Child display");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls overridden method in Child class
}
}
Output
Child display
2. Override Methods Cannot Be Overridden
Override methods in the subclass can be overridden again only if they are not marked as final or private. These methods are meant to be overridden in the subclass, but they cannot be further overridden in the subclass of that subclass. This ensures a specific behavior is maintained.
Example
class Parent {
protected void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
@Override
public void display() {
System.out.println("Child display");
}
}
class GrandChild extends Child {
// Uncommenting the next line will give a compile-time error
// @Override
// public void display() { // Error: Cannot override the final method from Child
// System.out.println("GrandChild display");
// }
}
public class Main {
public static void main(String[] args) {
Parent obj = new GrandChild();
obj.display(); // Calls overridden method in Child class
}
}
Output
Child display
3. Static Methods Cannot Be Overridden (Method Overriding vs Method Hiding)
In Java, methods declared with the static keyword cannot be overridden. Instead, they are subject to method hiding, where a static method in the subclass hides the static method in the parent class. This means the method's behavior is determined by the reference type, not the object type, as static methods are not dynamically dispatched.
Example
class Parent {
static void display() {
System.out.println("Static method in Parent");
}
}
class Child extends Parent {
static void display() { // This is method hiding, not overriding
System.out.println("Static method in Child");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls the static method in Parent
}
}
Output
Static method in Parent
4. Private Methods Cannot Be Overridden
Private methods in Java cannot be overridden, as subclasses do not inherit them. These methods are only accessible within the class where they are defined.
Example
class Parent {
private void display() {
System.out.println("Private method in Parent");
}
}
class Child extends Parent {
// This will not compile as private methods are not inherited
// @Override
// public void display() {
// System.out.println("Private method in Child");
// }
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
// obj.display(); // Compile-time error
}
}
Output
Compile-time error due to private method inheritance.
5. The Method Must Have the Same Return Type (or a Subtype)
When overriding a method, the return type must be the same as the parent method’s return type, or it can be a subtype of the original return type (known as covariant return type).
Example
class Parent {
Number display() {
return 42;
}
}
class Child extends Parent {
@Override
Integer display() { // Covariant return type, Integer is a subtype of Number
return 10;
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
System.out.println(obj.display()); // Calls the overridden method in Child
}
}
Output
10
Read More: Methods in C# |
6. Invoking the Overridden Method from the Subclass
Once you override a method in the subclass, you can call the overridden method from within the subclass using thesuper keyword in Javato refer to the parent class’s version of the method.
Example
class Parent {
void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
@Override
void display() {
super.display(); // Invoking overridden method from Parent
System.out.println("Child display");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls overridden method in Child
}
}
Output
Parent display
Child display
Overriding and Constructors
Constructors are not inherited by subclasses, so they cannot be overridden. However, a subclass can have its own constructor. The constructor of the parent class can be called from the subclass constructor using super().
This allows you to initialize the parent class’s state while adding new functionality in the subclass constructor.
Example
class Parent {
Parent() {
System.out.println("Parent class constructor");
}
}
class Child extends Parent {
Child() {
super(); // Calling the Parent class constructor
System.out.println("Child class constructor");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child(); // Calls the constructor of Child and then Parent
}
}
Output
Parent class constructor
Child class constructor
Read More: Constructor Overloading in Java |
Overriding and Exception Handling
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class. When it comes toexception handling in Java, thesubclass methodcan throw the same or more specific exceptions asthe parent method. However, the subclass method cannot throw broader or new exceptions that are not declared in the parent method.
Example
class Parent {
// Parent class method throws an exception
public void display() throws Exception {
System.out.println("Parent class method");
}
}
class Child extends Parent {
// Overridden method can throw the same or more specific exception
@Override
public void display() throws IllegalArgumentException {
System.out.println("Child class method");
}
}
public class Main {
public static void main(String[] args) {
try {
Parent obj = new Child();
obj.display(); // Calls overridden method in Child class
} catch (Exception e) {
System.out.println(e);
}
}
}
Output
Child class method
Overriding and Synchronized/strictfp Methods
Method overriding can also involve methods that are declared as synchronized or strictfp. While overriding a synchronized method, the subclass can maintain the synchronized modifier, but it is not mandatory. If the parent method is synchronized, the subclass method can either be synchronized or not. Similarly, the subclass can override a strictfp method, but it must retain the strictfp modifier or remove it.
Example
class Parent {
// Synchronized method in the parent class
public synchronized void display() {
System.out.println("Parent class synchronized method");
}
}
class Child extends Parent {
// Overriding the synchronized method in the subclass
@Override
public synchronized void display() {
System.out.println("Child class synchronized method");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls overridden synchronized method in Child class
}
}
Output
Child class synchronized method
Read More: Java Multithreading Interview Questions |
super Keyword in Java Overriding
Thesuper keyword in Javais used inmethod overridingto call a method from theparent classthat has been overridden in the subclass.It allows you to access and invoke methods or constructors from the parent class. In overriding, you can use super.methodName() to invoke the parent class method when it’s overridden in the subclass.
Example
class Parent {
public void display() {
System.out.println("Parent class display method");
}
}
class Child extends Parent {
@Override
public void display() {
super.display(); // Calling the Parent class method
System.out.println("Child class display method");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.display(); // Calls the overridden method in Child class
}
}
Output
Parent class display method
Child class display method
Access Specifiers in Method Overriding
In method overriding, the Access Modifiers in Javaofthe overridden method in the subclass must be as permissive or more permissive than the parent class method. For example, if a parent class method is `protected`, the subclass method can be `protected` or `public`, but it cannot be `private`. The goal is to ensure that the visibility of the overridden method is not reduced.
Example
class Parent {
// Parent method with protected access
protected void display() {
System.out.println("Parent class display method");
}
}
class Child extends Parent {
// Overriding method with public access (more permissive)
@Override
public void display() {
System.out.println("Child class display method");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls overridden method in Child class
}
}
Output
Child class display method
Understanding the Problem Without Method Overriding
Without method overriding, a subclass cannot provide its own specific implementation of a method already defined in its parent class. This leads to a lack of flexibility, as the subclass would inherit the parent class’s method without the ability to modify or extend its behavior. Method overriding helps to tailor functionality in subclasses, ensuring that they can respond to method calls in a way that fits their own needs.
Example of Method Overriding
Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class. The method in the child class should have the same name, return type, and parameters as the one in the parent class. Overriding is used to implement dynamic method dispatch, allowing the subclass to define its own version of the method.
Example
class Animal {
// Method in Parent class
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Overridden method in Child class
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal obj = new Dog(); // Creating object of Dog class
obj.sound(); // Calls overridden method in Dog class
}
}
Output
Dog barks
Real Example of Java Method Overriding
Method overriding is commonly used in real-world applications to define a general behavior in a parent class and customize it in subclasses. One such example is in a system where we model different types of employees, such as a full-time employee and a part-time employee. Both of them might calculate their salaries differently, but both use the same method name.
Example
class Employee {
// Method to calculate salary in Parent class
public void calculateSalary() {
System.out.println("Salary is calculated based on default criteria");
}
}
class FullTimeEmployee extends Employee {
// Overriding the calculateSalary method for FullTimeEmployee
@Override
public void calculateSalary() {
System.out.println("Full-Time Employee salary is calculated based on annual salary");
}
}
class PartTimeEmployee extends Employee {
// Overriding the calculateSalary method for PartTimeEmployee
@Override
public void calculateSalary() {
System.out.println("Part-Time Employee salary is calculated based on hourly rate");
}
}
public class Main {
public static void main(String[] args) {
Employee employee1 = new FullTimeEmployee(); // Creating object of FullTimeEmployee
Employee employee2 = new PartTimeEmployee(); // Creating object of PartTimeEmployee
employee1.calculateSalary(); // Calls overridden method in FullTimeEmployee
employee2.calculateSalary(); // Calls overridden method in PartTimeEmployee
}
}
Output
Full-Time Employee salary is calculated based on annual salary
Part-Time Employee salary is calculated based on hourly rate
Can We Override the Static Method?
Static methods cannot be overridden in Java. Instead, they are subject to method hiding, which means that the method in the child class will hide the method from the parent class, but it won't override it in the true sense. You cannot change the behavior of a static method in the way you can with instance methods.
Example
class Parent {
// Static method in Parent class
public static void display() {
System.out.println("Static method in Parent class");
}
}
class Child extends Parent {
// Hiding the static method in Child class (not overriding)
public static void display() {
System.out.println("Static method in Child class");
}
}
public class Main {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
obj1.display(); // Calls Parent's static method
obj2.display(); // Calls Parent's static method due to reference type
}
}
Output
Static method in Parent class
Static method in Parent class
Why Can We Not Override Static Methods?
In Java, you can't override static methods because they belong to the class, not to any specific instance. Static methods are resolved during compile-time, unlike instance methods, which are resolved at runtime. So, if you define a static method in a subclass with the same signature as the parent class, you're not overriding it but hiding it.
Example
class Parent {
// Static method in Parent class
public static void display() {
System.out.println("Parent static method");
}
}
class Child extends Parent {
// Static method with same signature in Child class
public static void display() {
System.out.println("Child static method");
}
}
public class Main {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
obj1.display(); // Calls Parent class static method
obj2.display(); // Calls Parent class static method, not Child class method
}
}
Output
Parent static method
Parent static method
Can We Override Java main() Method?
You cannot override the main() method in Java because it is a static method. Static methods belong to the class itself, not instances of the class, and overriding works only for instance methods. However, you can define another main() method with the same signature in a subclass, but it will hide the parent class's main method, not override it.
Example
class Parent {
// Main method in Parent class
public static void main(String[] args) {
System.out.println("Parent main method");
}
}
class Child extends Parent {
// Static method in Child class with same signature (method hiding)
public static void main(String[] args) {
System.out.println("Child main method");
}
}
public class Main {
public static void main(String[] args) {
Parent.main(args); // Calls Parent class main method
Child.main(args); // Calls Child class main method
}
}
Output
Parent main method
Child main method
Method Overloading Vs. Method Overriding
Method Overloading and Overriding in Java enable flexibility in method behavior.Method overloading in Java allows a class to define multiple methods with the same name but different parameters, while method overriding involves redefining a method in a subclass that is already defined in its parent class.
I have mentioned the comparison table below:
Aspect | Method Overloading | Method Overriding |
Definition | Defining multiple methods with the same name but different parameters. | Redefining a method from the superclass in a subclass with the same signature. |
Purpose | To perform different tasks using the same method name. | To change the behavior of a method in the subclass. |
Parameters | Methods must have different parameter types or a different number of parameters. | Methods must have the same parameter list as the method in the superclass. |
Return Type | It can have different return types. | It must have the same return type as the method in the superclass. |
Binding | Occurs at compile-time (Static Binding). | Occurs at runtime (Dynamic Binding). |
Inheritance | It is not required, as it can happen within the same class. | Requires Inheritance in Java(the subclass overrides the superclass method). |
Access Modifiers | There can be different access modifiers for each overloaded method. | Access modifiers in the subclass method cannot be more restrictive than the superclass method. |
Example | int add(int a, int b) and double add(double a, double b) | void display() { System.out.println("Child Class"); } in a subclass that overrides a void display() in the superclass. |
Java Access Modifiers with Method Overriding
When overriding methods, the access modifiers must follow these rules:
- The overridden method's access modifier should be the same or more permissive than the parent method's access modifier.
- Private methods cannot be overridden.
- Protected and public methods can be overridden with the same or more permissive modifiers.
Example
class Parent {
// Method with protected access modifier
protected void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
// Method with same protected access modifier
@Override
protected void display() {
System.out.println("Child display");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls overridden method in Child class
}
}
Output
Child display
Summary
This article explains Method Overriding in Java, where a subclass redefines an inherited method to enhance flexibility and achieve runtime polymorphism. It compares method overloading and method overriding and emphasizes its importance in Java inheritance.Master Java programming with the Scholarhat Java Programming Course and advance your skills.Explore specialized training in Java, ReactJS, and Python with Scholarhat Master Classes. Join now to boost your career!
Test Your Knowledge on Overriding in Java!
Q 1: What is the primary characteristic of method overriding in Java?
- (a) The method signature must be the same in both the parent and child class
- (b) The method in the parent class is private
- (c) The return type must be different in the parent and child class
- (d) The method in the parent class is static
Q 2: Can a subclass override a static method from the parent class?
- (a) Yes, it can override static methods
- (b) No, static methods cannot be overridden
- (c) Only public static methods can be overridden
- (d) Static methods can be overridden only if they are final
Q 3: What is the return type of an overriding method in Java?
- (a) It must be the same as the return type of the parent class method
- (b) It can be different from the parent class return type
- (c) It must be void
- (d) It must be a subclass of the return type in the parent class
Q 4: What happens if a method is marked as `final` in the parent class?
- (a) The method can still be overridden in the subclass
- (b) The method cannot be overridden in the subclass
- (c) The method will throw a compilation error if overridden
- (d) The subclass will inherit a default implementation
Q 5: Which of the following is true about method overriding in Java?
- (a) The method in the parent class can have a more restrictive access modifier than the child class
- (b) The overriding method must have the same or broader access level than the method in the parent class
- (c) The method in the child class must have a more restrictive access modifier
- (d) There is no restriction on the access modifiers of overridden methods