Java Programming Language
Table of content:
- History Of Java Programming Langauge
- Infographic For History Of Java
- What’s In The Name | History Of Java
- Key Features Of Java
- Advantages And Disadvantages Of Java
- The Version History Of Java Langauge
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is JDK?
- How To Download Java Development Kit (JDK) For Windows, MacOS, and Linux?
- Set Environment Variables In Java
- How To Install Java (JDK) On Windows 64-Bit Machine?
- How To Install Java (JDK) On Linux?
- How To Install Java (JDK) On macOS?
- How To Test Java Installation?
- How To Write Your First Java Program On Linux OS?
- Conclusion
- Frequently Asked Questions
Table of content:
- Java Programming Language | An Introduction
- 15 Key Features Of Java
- Write Once Run Anywhere (WORA) | Features Of Java
- Java Editions
- 5 New Features Of JAVA 8
- 5 New Features Of JAVA 11
- What Makes Java Popular?
- Conclusion
- Frequently Asked Questions
Table of content:
- What is Java?
- Advantages of Java
- Disadvantages of Java
Table of content:
- What Is Java Programming?
- Role Of Integrated Development Environments (IDEs) In Java Development
- 15 Best Java IDE For Developers
- In-Depth Comparison Table
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences Between Java And Core Java
- What Is Java?
- What Is Core Java?
- Applications Of Java
- Applications Of Core Java
- When To Use Java?
- When To Use Core Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Variables In Java Language?
- How To Declare Variables In Java Programs?
- How To Initialize Variables In Java?
- Naming Conventions For Variables In Java
- Types Of Variables In Java
- Local Variables In Java
- Instance Variables In Java
- Static Variables In Java
- Final Variables In Java
- Scope and Lifetime of Variables In Java
- Data Types Of Variables In Java (Primitive & Non-primitive)
- Java Variable Type Conversion & Type Casting
- Working With Variables In Java (Examples)
- Access Modifiers & Variables In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Identifiers In Java?
- Syntax Rules For Identifiers In Java
- Valid Identifiers in Java
- Invalid Identifiers in Java
- Java Reserved Keywords
- Naming Conventions & Best Practices For Identifiers In Java
- What Is An Identifier Expected Error In Java?
- Reasons The Identifier Expected Error Occurs
- How To Fix/ Resolve Identifier Expected Errors In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Data Types In Java?
- Primitive Data Types In Java
- Non-Primitive Data Types In Java
- Key Differences Between Primitive And Non-Primitive Data Types In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Operators In Java?
- Types Of Operators In Java
- Unary Operators In Java
- Arithmetic Operators In Java
- Assignment Operators In Java
- Relational Operators In Java
- Logical Operators In Java
- Bitwise Operators In Java
- Shift Operators In Java
- Increment & Decrement Operators In Java
- Ternary Operator In Java
- Instanceof Operator In Java
- Precedence & Associativity Of Java Operators
- Advantages & Disadvantages Of Operators In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Return Statement In Java?
- Use Cases Of Return Statements In Java
- Returning A Value From A Method In Java
- Returning A Class Object In Java
- Returning Void (No Value) In Java
- Advantages Of Using Return Statements In Java
- Limitations Of Using Return Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Keywords In Java?
- List Of Keywords In Java
- Detailed Overview Of Java Keywords With Examples
- What If When Keywords In Java Are Used As Variable Names?
- Difference Between Identifiers & Keywords In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Abstract Keyword In Java?
- Use Of Abstract Keyword In Java
- Abstract Methods In Java
- Abstract Classes In Java
- Advantages Of Abstract Keyword In Java
- Disadvantages Of Abstract Keyword In Java
- Abstract Classes Vs. Interfaces In Java
- Real-World Applications Of Abstract Keyword
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is throws Keyword In Java?
- How Does The throws Keyword Work?
- Throwing A Checked Exception Using throws In Java
- Throwing Multiple Exceptions Using throws In Java
- Throwing A Custom Exception Using throws In Java
- When To Use The throws Keyword In Java
- Difference Between throw and throws Keyword In Java
- Best Practices For Using The throws Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Volatile Keyword In Java?
- How Does Volatile Keyword In Java Work?
- Using Volatile Keyword In Java To Control Thread Execution
- Using Volatile Keyword In Java To Signal Between Multiple Threads
- Difference Between Synchronization And Volatile Keyword
- Common Mistakes And Best Practices While Using Volatile Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Super Keyword In Java
- Super Keyword In Java With Instance Variables
- Super Keyword In Java With Method Overriding
- Super Keyword In Java With Constructor Chaining
- Applications Of Super Keyword In Java
- Difference Between This And Super Keyword In Java
- Advantages Of Using Super Keyword In Java
- Limitations And Considerations Of Super Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding This Keyword In Java
- Uses Of This Keyword In Java
- Using This Keyword For Referencing Instance Variables
- Using This Keyword For Invoking A Constructor
- Using This Keyword For Invoking A Method
- Using This Keyword With Getters And Setters
- Difference Between This And Super Keyword In Java
- Best Practices For Using This Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is new Keyword In Java?
- Uses Of The new Keyword In Java
- Memory Management With new Keyword In Java
- Example 1: Creating An Object Of A Class Using new Keyword In Java
- Example 2: Creating An Array Using The new Keyword In Java
- Best Practices For Using new Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Transient Keyword In Java?
- Real-Life Example Of The Transient Keyword In Java
- When To Use The Transient Keyword In Java
- Example 1: Effect Of Transient Keyword On Serialization In Java
- Example 2: Skipping Sensitive Data During Serialization With Transient Keyword In Java
- Using Transient With Final Keyword In Java
- Using Transient With Static Keyword
- Difference Between Transient And Volatile Keyword In Java
- Advantages And Disadvantages Of Transient Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Static Keyword In Java?
- Characteristics Of Static Keyword In Java
- Static Variables In Java
- Static Method In Java
- Static Blocks In Java
- Static Classes In Java
- Static Variables Vs Instance Variables In Java
- Advantages Of Static Keyword In Java
- Disadvantages Of Static Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Static Method In Java?
- Use Cases Of Static Method In Java
- Using Static Method In Java To Create A Utility Class
- Using Static Method In Java To Implement The Singleton Design Pattern
- Difference Between Static And Instance Methods In Java
- Limitations Of Static Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Final Keyword In Java
- Final Variables In Java
- Final Methods In Java
- Final Classes In Java
- Difference Between Static And Final Keyword In Java
- Uses Of Final Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Difference Between final, finally, And finalize In Java
- What Is final Keyword In Java?
- What Is finally Keyword In Java?
- What Is finalize Keyword In Java?
- When To Use Which Keyword In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The extends Keyword In Java?
- Use Of extends Keyword In Java
- Using Java extends To Implement Single Inheritance
- Using Java extends With Interfaces (Default Methods)
- Overriding Using extends Keyword In Java
- Difference Between extends And implements In Java
- Real World Applications Of Extends Keyword In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Decision Making Statement In Java?
- If Statement In Java
- If-Else Statement In Java
- Else-If Ladder In Java
- Switch Statement In Java
- Ternary/Conditional Operator (?:) In Java
- Best Practices For Writing Decision Making Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Control Statements in Java?
- Decision-Making Control Statements In Java
- Looping Control Statements In Java
- Jump (Branching) Control Statements In Java
- Application Of Control Statements In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Break Statement In Java?
- Working Of The Break Statement In Java
- Using Java Break Statement With Loops
- Using Java Break Statement With Switch Statement
- Using Java Break Statement With Infinite Loops
- Common Pitfalls While Using Break Statements In Java
- Best Practices For Using The Break Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Switch Statement In Java?
- Working Of The Switch Statement In Java
- Example Of Switch Statement In Java
- Java Switch Statement With String
- Java Nested Switch Statements
- Java Enum In Switch Statement
- Java Wrapper Classes In Switch Statements
- Uses Of Switch Statement In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Syntax Of main() Method In Java
- public Specifier – Main Method In Java
- static Keyword – Main Method In Java
- void Return Type Of Main Method In Java
- The main Identifier – Main Method In Java
- String[] args In Main Method In Java
- The Role Of Java Virtual Machine (JVM)
- Running Java Programs Without The Main Method
- Variations In Declaration Of Main Method In Java
- Overloading The Main Method In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overriding In Java?
- Example Of Method Overriding In Java
- Ideal Use Cases Of Method Overriding In Java
- Rules For Method Overriding In Java
- Super Keyword & Method Overriding In Java
- Constructor & Method Overriding In Java
- Exception Handling In Method Overriding In Java
- Access Modifiers In Method Overriding In Java
- Advantages & Disadvantages Of Method Overriding In Java
- Difference Between Method Overloading Vs. Method Overriding In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Method Overloading In Java?
- Different Ways Of Method Overloading In Java
- Overloading The main() Method In Java
- Type Promotion & Method Overloading In Java
- Null Error & Method Overloading In Java
- Advantages Of Method Overloading In Java
- Disadvantages Of Method Overloading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Overloading And Overriding In Java (Comparison Table)
- What Is Method Overloading In Java?
- What Is Method Overriding In Java?
- Key Differences Between Overloading & Overriding In Java Explained
- Difference Between Overloading And Overriding In Java Code Example
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A One-Dimensional Array In Java?
- Key Characteristics Of One-Dimensional Arrays In Java
- Declaration Of One-Dimensional Array In Java
- Initialization Of One-Dimensional Array In Java
- Common Operations On One-Dimensional Array In Java
- Advantages Of One-Dimensional Arrays In Java
- Disadvantages Of One-Dimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Multidimensional Array In Java?
- Difference Between Single-Dimensional And Multidimensional Arrays In Java
- Declaring Multidimensional Arrays In Java
- Initializing Multidimensional Arrays In Java
- Accessing And Manipulating Elements In Multidimensional Arrays In Java
- Working Of Multidimensional Arrays With Jagged Arrays In Java
- Why Use Multidimensional Arrays In Java?
- Limitations Of Multidimensional Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Jagged Arrays In Java?
- Comparison With Regular Multi-Dimensional Arrays
- Declaring Jagged Arrays In Java
- Initialization Of Jagged Arrays In Java
- Printing Elements Of A Jagged Array In Java
- Accessing And Modifying Elements Of A Jagged Array In Java
- Advantages Of Jagged Arrays In Java
- Disadvantages Of Jagged Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Array Of Objects In Java?
- Declare And Initialize An Array Of Object In Java
- Example Of An Array Of Objects In Java
- Sorting An Array Of Objects In Java
- Passing Arrays Of Objects To Methods In Java
- Returning Arrays Of Objects From Methods In Java
- Advantages Of Arrays Of Objects In Java
- Disadvantages Of Arrays Of Objects In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Dynamic Array In Java?
- Why Use Dynamic Array In Java?
- What Is The Size And Capacity Of A Dynamic Array In Java?
- How To Create A Dynamic Array In Java?
- Managing Dynamic Data Input In Java
- Storing And Processing Real-Time Data In Java
- Use Cases Of Dynamic Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Return An Array In Java?
- How To Return An Array In Java
- Example 1: Returning An Array Of First N Squares
- Example 2: Doubling the Values of an Array
- Common Scenarios For Returning Arrays In Java
- Points To Remember When Returning Arrays In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding ArrayList In Java
- Differences Between Arrays And ArrayList In Java
- Returning An ArrayList In Java
- Common Use Cases For Returning An ArrayList In Java
- Pitfalls To Avoid When Returning An ArrayList In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Thread In Java?
- Thread Vs Process
- What is a Thread Life Cycle In Java?
- What Are Thread Priorities?
- Creating Threads In Java
- Java Thread Methods
- Commonly Used Constructors In Thread Class
- Thread Synchronization In Java
- Common Challenges Faced While Using Threads In Java
- Best Practices For Using Threads In Java
- Real-World Applications Of Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding Multithreading In Java
- Methods Of Multithreading In Java (Examples)
- Difference Between Multithreading And Multitasking In Java
- Handling Exceptions In Multithreading
- Best Practices For Multithreading In Java
- Real-World Use Cases of Multithreading In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Priority In Java?
- Built-In Thread Priority Constants In Java
- Thread Priority: Setter & Getter Methods
- Limitations Of Thread Priority In Java
- Best Practices For Using Thread Priority In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Thread Synchronization In Java?
- The Need For Thread Synchronization In Java
- Types Of Thread Synchronization In Java
- Mutual Exclusion In Thread Synchronization In Java
- Coordination Synchronization (Thread Communication) In Java
- Advantages Of Thread Synchronization In Java
- Disadvantages Of Thread Synchronization In Java
- Alternatives To Synchronization In Java
- Deadlock And Thread Synchronization In Java
- Real-World Use Cases Of Thread Synchronization In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Daemon Thread In Java?
- User Threads Vs. Daemon Threads In Java
- Methods For Daemon Threads In The Thread Class
- Creating Daemon Threads In Java
- Checking The Daemon Status Of A Thread
- Exceptions In Daemon Threads
- Limitations Of Daemon Threads In Java
- Practical Applications Of Daemon Threads In Java
- Common Mistakes To Avoid When Working With Daemon Threads In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Do Threads Need To Communicate?
- Understanding Inter Thread Communication In Java
- The wait() Method In Inter-Thread Communication
- The notify() Method In Inter-Thread Communication
- The notifyAll() Method In Inter-Thread Communication
- Difference Between wait() And sleep() Methods In Java
- Best Practices For Inter Thread Communication In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Factorial Concept
- Approaches To Implementing Factorial In Java
- Find Factorial In Java Using Iterative Approach (Using a Loop)
- Find Factorial In Java Using Recursive Approach
- Complexity Analysis Of Factorial Programs In Java
- Applications Of Factorial Program In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Understanding The Leap Year Concept
- Approach To Check A Leap Year In Java
- Alternative Approach To Check A Leap Year In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Difference Between JDK, JRE, and JVM?
- What Is JVM (Java Virtual Machine)?
- What Is JRE (Java Runtime Environment)?
- What Is JDK (Java Development Kit)?
- Understanding The Difference Between JDK, JRE, And JVM
- Comparison Table For Difference Between JDK, JRE, And JVM
- Conclusion
- Frequently Asked Questions
Table of content:
- Difference Between Abstraction And Encapsulation In Java
- Understanding Abstraction In Java
- Understanding Encapsulation In Java
- When To Use Abstraction And Encapsulation?
- Conclusion
- Frequently Asked Questions
Table of content:
- Differences Between Abstract Class And Interface In Java
- What Is An Abstract Class In Java?
- What Is An Interface In Java?
- When To Use An Abstract Class?
- When To Use Interface?
- Compatibility Between Abstract Class And Interface In Java
- Conclusion
- Frequently Asked Questions
Table of content:
- Error Vs. Exception In Java
- What Is Error In Java?
- What Is Exception In Java?
- Best Practices For Handling Exceptions In Java
- Why Errors Should Not Be Handled In Java?
- Conclusion
- Frequently Asked Questions
Table of content:
- Key Differences: Java Vs. JavaScript
- What Is Java?
- What Is JavaScript?
- Difference Between Java And JavaScript Explained
- Conclusion
- Frequently Asked Questions
Table of content:
- Brief Introduction To C++
- Brief Introduction To Java
- Difference Between C++ and Java
- Overview & Features Of C++ Language
- Overview & Features of Java Language
- Example of C++ and Java Program
- Key Difference Between C++ And Java Explained
- Similarities Between Java Vs. C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Basic Java interview questions and answers
- Intermediate Java interview questions and answers
- Advanced Java interview questions and answers
Table of content:
- Difference between core Java and advanced Java
- Important Core Java Questions
- Tips for Preparing for Core Java
Method Overriding In Java | Rules, Use-Cases & More (+Examples)

Imagine you have an old family recipe passed down for generations. And while you follow most of it, you like to add a few of your own touches—a pinch of spice here, a twist in flavor there—to make it uniquely yours. This is what method overriding in Java is about, but for methods/functions.
In technical terms, you inherit a method from a superclass (like the recipe) but redefine it in your subclass (your own version) to suit specific needs. In this article, we will discuss method overriding in Java in detail, including its rules, implementations, use cases, pitfalls, and more, with proper code examples.
What Is Method Overriding In Java?
Method overriding is a powerful concept in Java that allows a subclass (or child class) to redefine a method inherited from its superclass (or parent class). This is especially useful when the implementation of the inherited method doesn’t fully serve the subclass's needs.
- By overriding, the subclass provides its own implementation of a method that already exists in the superclass, customizing its behavior as required.
- In method overriding, both the superclass and the subclass contain a method with the same name/ identifer, return type, and parameters (signature).
- When the subclass redefines this method, it effectively "overrides" the version in the superclass, replacing the inherited behavior with its own.
This allows objects to exhibit different behaviors based on their runtime types—a concept known as runtime polymorphism or dynamic method dispatch. With runtime polymorphism, the method that gets executed is chosen at runtime, depending on the object’s actual type.
Syntax For Method Overriding In Java:
class Superclass {
// Superclass method
returnType methodName() {
// Method implementation in superclass
}
}class Subclass extends Superclass {
// Subclass method overriding the superclass method
@Override
returnType methodName() {
// Method implementation in subclass
}
}
Here,
- The Superclass is the base class containing the method to be overridden.
- The Subclass is the child class that extends the superclass and provides a custom implementation.
- The returnType specifies the type of value returned, like int or String.
- methodName is the identifier or name of the method, matching the one in the superclass.
- The @Override annotation signals that the method in the subclass is intended to override one in the superclass, ensuring the method signatures match exactly.
In the syntax above, the superclass method provides default behavior, while the subclass method customizes it to meet specific needs.
Example Of Method Overriding In Java
Now that we've got some idea about the syntax of method overriding in Java, let’s look at an example that showcases how method overriding works in real code scenarios.
Code Example:
class Parent {
void display() {
System.out.println("Inside Parent's display method.");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Inside Child's display method.");
}
}
class Main {
public static void main(String[] args) {
Parent parent = new Parent(); // Creating Parent object
Child child = new Child();Â Â // Creating Child object
parent.display(); // Calls Parent's display method
child.display();Â // Calls Child's display method
// Polymorphism: Parent reference pointing to Child object
Parent polymorphicRef = new Child();
polymorphicRef.display(); // Calls Child's display method due to dynamic method dispatch
}
}
Output:
Inside Parent's display method.
Inside Child's display method.
Inside Child's display method.
Explanation:
In the simple Java code example,
- We begin by defining a parent/ base class called Parent, containing a simple void method, display().
- The method uses println() method to display the string/ message– "Inside Parent's display method" when called.
- Then, we create a child/ subclass called Child that extends the Parent class. Inside:
- We override the display() method from the Parent class.
- First, we use the annotation/ keyword @Override to indicate that this function must override another one.
- Then, we define the display() method, which prints the message– "Inside Child's display method." when called on a Child object instead of the parent's version.
- Next, we define the Main class, which serves as the entry point for executing our program. Inside, we create objects for both classes, i.e., Parent (parent) and Child (child), using the new operator with respective constructors.
- We then call the display() method using the parent object, which invokes the method defined in the Parent class and prints the respective message.
- Then, we call the display() method using the child object, which invokes the overridden method in the Child class, printing the child-specific message.
- This is a direct demonstration of method overriding in Java, where the child class provides a specific implementation for a method that is already defined in the parent class.
- Now, we introduce polymorphism, for which we create a reference of type Parent but assign it an object of type Child.
- This is allowed because Child is a subclass of Parent, meaning a Child object can be referred to by a Parent reference.
- Then, when we call the display() method using the polymorphicRef reference, it might seem like the display() method of the Parent class should be called.
- However, due to dynamic method dispatch, Java determines at runtime which method to call based on the actual object type, not the reference type. Since polymorphicRef points to an instance of Child, the overridden display() method in Child is called, and it prints the child's message.
Want to expand your knowledge of Java programming? Check out this amazing course and become the Java developer of your dreams.
Ideal Use Cases Of Method Overriding In Java
Method overriding can be ideal in scenarios where we want to provide a specific implementation of a method in a subclass that is different from the implementation in the respective superclass. Following are some of the specific use cases where method overriding in Java can prove to be beneficial:
- Implementing Runtime Polymorphism: Method overriding is a core mechanism behind runtime polymorphism in Java. It allows a subclass to provide its own implementation of a method that exists in its superclass. This makes it possible for objects of different types to respond to the same method call in different ways, depending on their actual type at runtime.
- Customizing Behavior: Method overriding enables customization of behavior inherited from a superclass. If a subclass needs to alter or extend the functionality of an inherited method, it can simply override the method with a more specialized implementation suited to its requirements.
- Enhancing Reusability: Method overriding in Java allows us to reuse the same method name and signature (from superclass) across subclasses. This promotes code reusability by allowing subclasses to retain the same method interface but customize the functionality, reducing code duplication.
- Implementing Abstract Methods: If a class extends an abstract class or implements an interface, it must provide concrete implementations for all abstract methods defined in the superclass. Method overriding in Java helps implement these abstract methods, making the subclass a concrete class.
- Enabling Dynamic Dispatch: With method overriding, Java achieves dynamic dispatch, where the JVM determines which method implementation to invoke based on the actual type of the object at runtime. This flexibility is essential in scenarios where you manipulate objects of different subclasses via a common superclass or interface reference.
- Framework and Library Extensions: Method overriding in Java is commonly used in frameworks and libraries to extend or customize the behavior of existing classes without modifying their source code. By overriding methods in these predefined classes, we can create custom components or behaviors that fit the specific needs of our application.
In short, method overriding enables customization, extensibility, and polymorphism in Java applications, making the codebase more flexible, scalable, and easier to maintain.
Rules For Method Overriding In Java
Method overriding is essential for creating flexible, large-scale programs, enabling subclasses to modify inherited methods to meet specific needs. While the concept is crucial, it's also vital to understand the rules that govern how a subclass can override a method from its superclass.
These rules provide a structure for modifying inherited methods without breaking functionality, ensuring that your code remains adaptive and cohesive. Here are key rules you must note when working with method overriding in Java:
1. Access Modifiers & Method Overriding In Java
When overriding a method, we cannot reduce the visibility of the method in the subclass. The order of access modifiers from most restrictive to least restrictive is:
private < default (package-private) < protected < public.
This means you can increase the visibility (e.g., from protected to public) but not decrease it (e.g., from public to protected). We will discuss this in detail in a later section.
2. Method Signature For Superclass & Subclass Must Be Same For Method Overriding In Java
The method signature (name, return type, and parameter list) in the subclass must be identical to the one in the superclass.
3. Exceptions & Method Overriding In Java
When a subclass method overrides a parent method, it can throw unchecked exceptions (like RuntimeException), even if the parent method doesn’t throw any exceptions. However, it should not throw new checked exceptions (like IOException) or exceptions that are more general than those thrown by the parent method. The overriding method can throw fewer or more specific exceptions than the parent, which allows the subclass to limit the types of exceptions it throws compared to the parent.
4. Constructors Cannot Be Overridden
In Java, constructors are not inherited by subclasses, so they cannot be overridden. Each class must define its own constructor, and while a subclass can call a superclass constructor using super(), it doesn’t override it.
5. Final Methods Cannot Be Overridden In Java
The final keyword can be used to mark a method as final, meaning it cannot be overridden in any subclass. This ensures that the method’s implementation remains consistent across all subclasses.
Suppose you have a superclass Shape with a final method calculateArea(), to calculate the area of the shape. Let’s see what happens when we try to override this method in a subclass.
Code Example:
class Shape {
// Final method to calculate the area
public final double calculateArea() {
return 0.0; // Default implementation for an unknown shape
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
// Attempting to override the final method, which is not allowed
// Error: Cannot override the final method from Shape
public double calculateArea() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5.0);
System.out.println("Area of circle: " + circle.calculateArea());
}
}
Output:
Main.java:17: error: calculateArea() in Circle cannot override calculateArea() in Shape
public double calculateArea() {
^
overridden method is final
1 error
Explanation:
The method calculateArea() in the Shape class is marked as final, which prevents any subclass from overriding it. Trying to override it in the Circle subclass results in a compilation error.
6. Static Methods Cannot Be Overridden
Static methods are resolved at compile time and are tied to the class, not the instance of the class. As a result, static methods cannot be overridden. If a subclass defines a static method with the same signature as a static method in its superclass, it's considered method hiding, not overriding.
Say a superclass Animal has a static method eat() that represents a general eating behavior for all animals. We then have a subclass Dog that attempts to override eat() with a non-static method eat() to represent a specific eating behavior for dogs. The Java program example below highlights how this works.
Code Example:
class Animal {
public static void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
// Attempting to override the static method with a non-static method is not allowed
// Error: This static method cannot hide the instance method from Animal
public void eat() {
System.out.println("Dog eats bones");
}
}
class Main {
public static void main(String[] args) {
Animal.eat(); // Expected outputs: Animal eats food
Dog dog = new Dog();
dog.eat(); // Expected output: Dog eats bones
}
}
Output:
Main.java:10: error: eat() in Dog cannot override eat() in Animal
public void eat() {
^
overridden method is static
1 error
Explanation:
In the example above, the Dog class tries to override the static method eat(), which is not allowed in Java. Static methods belong to the class itself and do not support polymorphism. The error message indicates that we can't override static methods with instance methods.
7. Private Methods Can Not Be Overridden
Private methods, that is, methods defined with the private access specifier, are not visible to subclasses. So, they cannot be overridden. If a subclass defines a method with the same signature as a private method in the superclass, it’s treated as a new, separate method, not an override. The simple Java program example below illustrates this concept.
Code Example:
class Superclass {
private void privateMethod() {
System.out.println("Private method in superclass.");
}
public void callPrivateMethod() {
privateMethod();
}
}
class Subclass extends Superclass {
private void privateMethod() {
System.out.println("Private method in subclass.");
}
}
class Main {
public static void main(String[] args) {
Superclass obj = new Subclass();
obj.callPrivateMethod(); // Output: Private method in superclass
}
}
Output:
Private method in superclass.
Explanation:
Even though Subclass has a method privateMethod() that has the same signature as the one in Superclass, it doesn't override it because private methods are inaccessible to subclasses. When calling callPrivateMethod(), the method from Superclass is executed, as private methods in Superclass are not overridden.
8. The Overriding Method Must Have the Same Return Type (or Subtype)
The return data type of the overriding method in Java subclasses must be the same as, or a subtype of, the return type of the overridden method in the superclass. This feature, known as the covariant return type, was introduced in Java 5.0.
Code Example:
class Animal {
Animal getAnimal() {
System.out.println("Animal");
return new Animal();
}
}
class Dog extends Animal {
@Override
Dog getAnimal() {
System.out.println("Dog");
return new Dog();
}
}
class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.getAnimal();
}
}
Output:
Dog
Explanation:
In this example, the method getAnimal() in Dog returns a Dog object, which is a subtype of Animal. This is a valid example of covariant return types, where the overriding method can have a more specific return type than the overridden method in the superclass.
Here is your chance to top the leaderboard while practising your coding skills: Particiapte in the 100-Day Coding Sprint at Unstop.
Super Keyword & Method Overriding In Java
In Java, the super keyword is used to refer to the superclass of the current object, allowing a subclass to access superclass members directly. This can include calling superclass methods ( despite overriding the same method), accessing superclass variables, and invoking superclass constructors.
Syntax:
super.methodName(); // Calling superclass method
super.variableName; // Accessing superclass variable
super(); // Calling superclass constructor
Use Cases Of super Keyword In Method Overriding In Java
The primary use cases of super keyword include the following three situations:
1. Invoking Immediate Parent Class Method
When a subclass overrides a method, the super keyword can be used to call the overridden method from the parent class. This is particularly useful if you want to extend or modify the behavior of the parent method while still retaining access to its original functionality.
Code Example:
class Parent {
void display() {
System.out.println("Parent class method");}
}
class Child extends Parent {
@Override
void display() {
super.display(); // Calls the display() method of the Parent class
System.out.println("Child class method");}
}
class Main {
public static void main(String[] args) {
Child child = new Child();
child.display();}
}
Output:
Parent class method
Child class method
Explanation:
In the Java example code,
- We first define a base class Parent with a method display() that simply prints the message– "Parent class method" to the console.
- Then, we define a child class, Child that extends the Parent class and overrides the display() method.
- Inside this overriding method, we use the super keyword to call the display() method from Parent (via super.display()), the immediate parent class of Child.
- In the Main class, we first create an instance of Child using the new operator with constructor and then use it to call the display() method.
- As a result, the display() method inside Child first invokes the display() method from the parent, printing the message– ‘Parent class method’.
- It then prints the message– ‘Child class method’ as this is part of the method’s own definition.
This demonstrates the use of the super keyword to invoke the method of the immediate parent class. In this case, it allows the Child class to call and augment the behavior of the Parent class method.
2. Invoking The Parent Class Constructor
The super() keyword can also be used to call the parent class's constructor. This is particularly useful if the parent class does not have a no-argument constructor or if you want to ensure proper initialization of inherited members. If the parent class lacks a no-argument constructor and you don't explicitly call another constructor with super(), the code will not compile.
Code Example:
class Parent {
Parent() {
System.out.println("Parent class constructor");
}
}
class Child extends Parent {
Child() {
super(); // Calls the constructor of the Parent class
System.out.println("Child class constructor");
}
}
class Main {
public static void main(String[] args) {
Child child = new Child();
}
}
Output:
Parent class constructor
Child class constructor
Explanation:
In the Java code example,
- We define the base class Parent with a constructor Parent() that prints "Parent class constructor" when invoked.
- Then, we define a child class, Child that extends the Parent class, which means it inherits from Parent.
- The Child class has its own constructor, Child(), inside which–
- We use the super() keyword to explicitly call the constructor of the immediate parent class (Parent class in this case).
- This is necessary if you want to explicitly call a specific constructor in the parent class (especially a parameterized one), as Java does not automatically call it if it’s not the default (no-argument) constructor.
- Without an explicit super() call, Java will only attempt to call the no-argument constructor in the parent class.
- After calling super(), the child class constructor continues its execution and prints "Child class constructor".
- In the Main class, we create an instance of Child, using the new operator with the Child() constructor.
- This triggers the constructor chain starting from the Parent class constructor and then the Child class constructor.
- The output demonstrates the use of the super keyword to invoke the immediate parent class constructor explicitly, ensuring proper initialization of the parent class before the child class constructor executes.
3. Referring To Immediate Parent Class Instance Variable
Although not directly related to method overriding, super can also refer to variables in the parent class. This is handy when the subclass has a variable with the same name, and you want to clarify which one to use.
Constructor & Method Overriding In Java
As discussed in the rules section above, constructors are special methods used to initialize new objects. They share the class name and don’t return any value, not even void. Constructors differ from other methods because they aren’t inherited and therefore cannot be overridden in subclasses.
- However, constructors in a parent class still play a role when creating a subclass object.
- When a subclass object is instantiated, Java ensures that a parent constructor is called first.
- This can be done either implicitly (via Java’s default constructor call if no specific constructor is defined) or explicitly using super() in the subclass constructor.
- If no constructor is defined in the subclass, Java automatically calls a no-argument constructor of the superclass using super(). If the superclass lacks a no-argument constructor, we must manually call one of its parameterized constructors using super().
To provide different constructors in a subclass, we can define constructors with various parameters and explicitly call specific superclass constructors. This approach ensures that we can initialize inherited members correctly, using the parent class’s initialization logic when necessary.
Exception Handling In Method Overriding In Java
In Java, exception handling allows subclasses to either maintain, restrict, or expand the exceptions thrown by an overridden method from the superclass. This control is governed by rules that help ensure subclasses don't introduce unexpected checked exceptions. Let's break down how exceptions, particularly checked and unchecked, interact with method overriding in Java language.
Checked Exceptions In Method Overriding In Java
When a method in a superclass throws a checked exception (like IOException), any overriding method in a subclass must adhere to certain rules:
- Same Exception: The subclass can throw the exact same checked exception.
- Subclass Exception: The subclass can throw a more specific (subclass) checked exception.
- No Exception or Broader Exception: Alternatively, the subclass can choose not to throw any checked exception or throw an unchecked exception instead.
Example: Checked Exception Handling with Method Overriding in Java
Let’s consider a scenario where we have a superclass FileProcessor with a processFile() method that reads files and may throw an IOException. In a subclass TextFileProcessor, we override processFile() to add specific behavior, such as verifying if the file is a text file, while handling potential file-related exceptions.
Code Example:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
// Superclass
class FileProcessor {
public void processFile(File file) throws IOException {
// Read the file
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
// Subclass
class TextFileProcessor extends FileProcessor {
@Override
public void processFile(File file) throws IOException {
// Override method and throw a checked exception
super.processFile(file); // Calls the superclass method for default processing
// Additional processing specific to text files
if (!file.getName().endsWith(".txt")) {
throw new IOException("Invalid file format");
}
// Process the text file
}
}
class Main {
public static void main(String[] args) {
File file = new File("example.txt");
TextFileProcessor processor = new TextFileProcessor();
try {
processor.processFile(file);
} catch (IOException e) {
System.out.println("Error processing file: " + e.getMessage());
}
}
}
Output:
File not found: example.txt (No such file or directory)
Explanation:
In the example Java code,
- We first define a superclass FileProcessor containing a method processFile() that takes a File object and reads its contents.
- By definition, this method can throw an IOException, if needed. It includes error handling for FileNotFoundException to handle cases where the file is not found.
- We then define a subclass TextFileProcessor that extends FileProcessor. It overrides the processFile() method to provide additional processing specific to text files.
- In the overriding method, we first call the overridden method from superclass using the super() keyword to perform the default file processing.
- We use an if-statement to add specific behavior by checking if the filename ends in the extension ‘.txt’. If it does not, we throw an IOException with a message indicating invalid format for text files.
- In the Main class, we first create a File object for ‘example.txt’ and an instance of TextFileProcessor to process the file.
- Since the processFile() method in the superclass FileProcessor throws an IOException, the overriding method in TextFileProcessor must also declare that it throws IOException or a superclass of IOException, which is done using the throws IOException clause in its method signature.
- The output of the program (when ‘example.txt’ does not exist) demonstrates the error message ‘File not found: example.txt (No such file or directory)’ indicating that the FileNotFoundException was caught in the superclass method.
This is how if the example.txt file doesn’t exist or doesn’t match the expected format, an exception is thrown and handled, ensuring the program doesn’t crash unexpectedly. It demonstrates how the subclass respects the checked exception IOException and builds on it without introducing new exceptions outside what the superclass declared. This preserves compatibility with the superclass’s exception handling while extending functionality.
Unchecked Exceptions In Method Overriding In Java
The Java language is much more lenient when it comes to unchecked exceptions in method overriding. These exceptions, which are subclasses of RuntimeException (such as IllegalArgumentException), don’t need to be declared or handled by the superclass. This allows the subclass to introduce or omit any unchecked exceptions in its overridden methods, giving it more flexibility for cases where runtime issues may arise.
Example: Unchecked Exception Handling with Method Overriding in Java
In the Java example below, we modify processFile to throw an unchecked IllegalArgumentException if the file path is invalid.
Code Example:
// Superclass
class FileProcessor {
public void processFile(String filePath) {
if (filePath == null || filePath.isEmpty()) {
throw new IllegalArgumentException("File path cannot be null or empty");
}
// Process the file
}
}
// Subclass
class TextFileProcessor extends FileProcessor {
@Override
public void processFile(String filePath) {
super.processFile(filePath); // Call superclass method
// Additional processing specific to text files
if (!filePath.endsWith(".txt")) {
throw new IllegalArgumentException("Invalid file format");
} else {
// Process the text file
System.out.println("Processing the text file...");}
}
}
class Main {
public static void main(String[] args) {
TextFileProcessor processor = new TextFileProcessor();
try {
processor.processFile("example.txt");
} catch (IllegalArgumentException e) {
System.out.println("Error processing file: " + e.getMessage());}
}
}
Output:
Processing the text file...
Explanation:
In the example Java program,
- We define a superclass FileProcessor with a method processFile() that validates the file path and throws an IllegalArgumentException if the path is null or empty.
- Then, we define a subclass TextFileProcessor class that extends FileProcessor and overrides the processFile() method to additional checks specific to text files.
- In the overriding method, we first call the superclass method using super to ensure the original validation for null or empty file paths is performed.
- It then uses an if-else statement to check if the file path ends with ‘.txt’. If it does, then it prints a message indicating that the text file is being processed.
- If not, then it throws another IllegalArgumentException.
- Since IllegalArgumentException is an unchecked exception, there are no restrictions on its use. The subclass can freely add checks without the need for the superclass to account for this exception, as it doesn't require a declaration in the method signature (because it’s unchecked).
- In the Main class, we create an instance of TextFileProcessor and call its processFile() method with a valid text file path.
- As shown in the output, since the file path is valid, the program prints a message indicating that the text file is being processed.
This approach allows TextFileProcessor to handle runtime-specific cases more flexibly while retaining the original method’s validation.
Access Modifiers In Method Overriding In Java
As mentioned in the rules section, one crucial point to remember when method overriding in Java is that we cannot reduce the visibility of the method in the subclass. That is, the access modifier of the method in the subclass must be equal to or more permissive than the access modifier of the method in the superclass.
In other words, we can increase the visibility (e.g., from protected to public), but we cannot decrease it (e.g., from public to protected). The hierarchy of access modifiers, from the most restrictive to the least restrictive, is as follows:
private < default (package-private) < protected < public
This rule ensures that subclasses don't inadvertently restrict access to functionality that is accessible in the parent class. Let's explore this with two examples—one demonstrating reduced visibility and another showing increased visibility with method overriding in Java.
Example 1: Reducing Visibility (Not Allowed) In Method Overriding In Java
Consider a superclass Animal with a method makeSound() declared as public. The subclass Dog tries to override makeSound() with a more restrictive access modifier, protected. This is not allowed in Java, as it violates the rule of reducing visibility.
Code Example:
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Attempting to reduce visibility from public to protected (Not Allowed)
@Override
protected void makeSound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound(); // Compilation error due to access modifier mismatch
}
}
Output:
Main.java:10: error: makeSound() in Dog cannot override makeSound() in Animal
protected void makeSound() {
^
attempting to assign weaker access privileges; was public
1 error
Explanation:
In the Java example,
- The method makeSound() in the Animal class is public, meaning it is accessible everywhere.
- In the Dog class, the method is overridden with protected visibility, which is less permissive than public. Java does not allow this because it would reduce the method's visibility from what was declared in the superclass.
- This results in a compilation error indicating that we are trying to assign weaker access privileges.
Example 2: Increasing Visibility (Allowed) In Method Overriding In Java
Now, let's consider increasing the visibility of the overridden method. In this example, we have a superclass Animal with a protected method makeSound(). The subclass Dog overrides this method with a public access modifier, which is allowed.
Code Example:
class Animal {
protected void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Increasing visibility from protected to public (Allowed)
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound(); // Output: "Dog barks"
}
}
Output:
Dog barks
Explanation:
In the example,
- The method makeSound() in the Animal class is protected, meaning it is accessible within the same package and by subclasses.
- In the Dog class, the method is overridden with public visibility, which is more permissive. This is allowed because the method in the subclass is more accessible than the method in the superclass.
- This results in the program running without errors, and the output is "Dog barks", demonstrating that the subclass method is invoked.
Advantages & Disadvantages Of Method Overriding In Java
Method overriding is a key feature in Java, enabling subclasses to provide their own implementation of a method inherited from a superclass. While this powerful feature offers several benefits, it also comes with its set of challenges.
Advantages Of Method Overriding In Java
- Polymorphic Behavior: Method overriding enables polymorphism, allowing objects of different classes to be treated as objects of a common superclass. This promotes code reusability and enhances the flexibility of design by enabling dynamic method invocation based on the object type at runtime.
- Enhanced Flexibility: Subclasses can customize inherited methods to meet their specific needs, offering greater control over functionality. This flexibility of method overriding in Java allows for specialization while still maintaining the foundation provided by the superclass.
- Improved Readability and Maintainability: By overriding methods, subclasses focus only on their custom behavior without altering the superclass. This separation of concerns results in cleaner, more modular code, which is easier to read and maintain.
- Code Reusability: With method overriding in Java, subclasses can reuse the superclass method's structure, reducing the need to duplicate code. This increases efficiency and ensures consistency, making the codebase easier to maintain.
- Run-Time Polymorphism: Method overriding is resolved at runtime based on the actual type of the object, enabling dynamic method dispatch. This allows Java to choose the appropriate method to call based on the object that invokes the method, making the application more flexible.
Disadvantages Of Method Overriding In Java
While method overriding offers many benefits, it also has some limitations and challenges that we should be aware of.
- Complexity and Overhead: While method overriding brings flexibility, it can also introduce complexity, especially in large class hierarchies. This is because managing interactions between superclass and subclass methods requires careful planning to avoid unexpected behavior or bugs.
- Fragility: Changes to the superclass method (e.g., modifying the method signature or behavior) can inadvertently affect the behavior of overridden methods in subclasses. This can lead to fragile code where a small change in the superclass has far-reaching consequences on the subclasses.
- Potential for Misuse: When used incorrectly, method overriding in Java programs can violate the principle of encapsulation, leading to hard-to-understand and poorly maintained code. Overriding methods unnecessarily or inappropriately can confuse developers and make the codebase harder to manage.
- Performance Impact: The use of dynamic method dispatch in method overriding introduces a slight performance overhead compared to static method invocation. While this impact is usually minimal and typically not noticeable in most applications, it can be a concern in performance-sensitive scenarios.
- Limitations in Constructors: Constructors cannot be overridden in Java, limiting the flexibility of subclass initialization logic. While constructors can be overloaded, they cannot be overridden like regular methods, which can be a constraint in some cases.
Difference Between Method Overloading Vs. Method Overriding In Java
Method overloading and method overriding are two important concepts in Java that involve the declaration and use of methods. While both concepts involve redefining methods, they are used in different contexts and have distinct characteristics. The table below highlights the difference between method overloading and method
Feature |
Method Overloading |
Method Overriding |
Definition |
Defining multiple methods with the same name but different parameters in the same class. |
Redefining a method in a subclass with the same signature as in the superclass. |
Purpose |
To allow a method to perform different tasks based on different parameters. |
To provide a new implementation of an inherited method. |
Resolution |
Resolved at compile-time based on method signature. |
Resolved at runtime based on the object's actual type. |
Return Type |
May or may not match the original method's return type. |
Must have the same return type as the overridden method. |
Exceptions |
No restrictions on thrown exceptions. |
Cannot throw broader checked exceptions than the superclass method. |
Inheritance |
Not related to inheritance. Methods are in the same class. |
Requires inheritance; method must exist in the superclass. |
Static Methods |
Can be overloaded. |
Cannot be overridden, but can be hidden. |
Final Methods |
Can be overloaded. |
Cannot be overridden. |
Private Methods |
Can be overloaded. |
Cannot be overridden. |
Need more guidance on how to become a Java developer? You can now select an expert to be your mentor here.
Conclusion
Method overriding in Java is a powerful feature that allows subclasses to provide their own implementation of methods inherited from the superclass. This ability to override methods makes code more adaptable, as it tailors inherited behaviors to meet specific needs.
- In this article, we covered the essential rules governing method overriding, such as the importance of method signatures, the role of access modifiers, and the use of the super keyword.
- We also explored the unique case of constructors, which cannot be overridden, and delved into the relationship between method overriding and exception handling.
- Specifically, we discussed the importance of preserving exception hierarchies when overriding methods that throw checked exceptions.
Ultimately, method overriding empowers developers to create new behaviors in subclasses while maintaining compatibility with their parent class. By enhancing code flexibility and simplifying maintenance, method overriding is an invaluable tool in a Java developer's toolkit.
Also read: Top 100+ Java Interview Questions And Answers (2024)
Frequently Asked Questions
Q. How does method overriding in Java relate to the Liskov Substitution Principle (LSP)?
Method overriding is closely tied to the Liskov Substitution Principle (LSP), which is one of the five SOLID principles of object-oriented design. The LSP states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. In other words, a subclass should be able to substitute for its superclass without changing the behavior expected by the client code.
- Method overriding ensures compliance with LSP by allowing subclasses to provide specific implementations of superclass methods while maintaining the same interface.
- A subclass must adhere to the expected behavior of the superclass’s method, ensuring that client code remains unaffected when a subclass instance replaces a superclass instance.
Q. Can a subclass declare a method to throw a checked exception that the superclass method doesn't declare?
No, a subclass cannot declare a method to throw a checked exception that isn’t declared by the superclass method it overrides.
This rule ensures consistency with the superclass's contract, preventing subclasses from introducing new exceptions that the superclass does not handle. The compiler enforces this, ensuring that any exception handling remains predictable and adheres to the superclass’s specifications.
Q. What is dynamic method dispatch? How is it related to method overriding in Java?
Dynamic method dispatch is a mechanism in Java that determines which method to invoke at runtime based on the actual type of the object, not the reference type. This mechanism is a core feature of polymorphism (object-oriented programming) and is directly related to method overriding in Java programming.
- When a subclass overrides a method, dynamic dispatch ensures that the correct version of the method is called at runtime based on the object's type.
- This allows subclasses to provide specialized behavior for inherited methods and have the correct implementation called based on the type of the object.
This is how dynamic method dispatch is achieved through method overriding in Java.
Q. What is the role of the @Override annotation in method overriding in Java? When should it be used?
The @Override annotation indicates that a method is overriding a method from its superclass.
- While optional, it is a best practice to use it because it helps the compiler check for errors, such as mismatched method signatures.
- If the method signature does not match a method in the superclass, the compiler will generate an error.
- Using @Override also improves code readability, making it clear to other developers that the method is meant to override a superclass method, thereby improving maintainability and reducing potential bugs.
Q. Can a subclass define an overloaded method that is also a valid override of a superclass method?
Yes, a subclass can define an overloaded method that also overrides a superclass method. Method overloading and method overriding are independent concepts in Java, so it is possible for a subclass to overload a method with the same name but different parameter lists, while still overriding the original method. Let’s understand it through the following example:
Code Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Overloaded method
void makeSound(String type) {
System.out.println("The dog's bark is " + type +".");
}
// Overriding method
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Calls the overridden method
myDog.makeSound("loud"); // Calls the overloaded method
}
}
Output:
Dog barks
The dog's bark is loud.
Here, we have a class Animal with a method named makeSound(), which is both overloaded and overridden in the child class Dog. The overloaded method expects an argument of String type, and the overridden method re-implements the logic according to the dog class.
Q. Explain the concept of method hiding in Java. How does it differ from method overriding?
Method hiding occurs when a subclass defines a static method with the same signature as a static method in the superclass. Unlike method overriding, which happens at runtime with instance methods, method hiding involves static methods and is determined at compile-time.
The table below highlights the difference between method hiding and method overriding in Java.
Feature |
Method Hiding |
Method Overriding |
Definition |
Defining/ declaring a static method in a subclass with the same signature as a static method in the superclass. |
Redefining an instance method in a subclass with the same signature as an instance method in the superclass. |
Resolution |
Compile-time |
Runtime |
Purpose |
Provide a new implementation of a static method in a subclass that hides the static method in its superclass. |
Provide a new behavior for an instance method, already defined in the superclass. |
Inheritance |
Not influenced by inheritance |
Requires inheritance (subclassing) |
Access Control |
Can change visibility |
Cannot reduce visibility |
Final Methods |
Can hide final methods |
Cannot override final methods |
Private Methods |
Can hide private methods |
Cannot override private methods |
Q. Can a subclass call the overridden method from within the overriding method?
Yes, a subclass can call the overridden method from the subclass using the super keyword. This allows the subclass to augment or extend the functionality of the method in the superclass rather than completely replacing it.
For example, a subclass can call the superclass method within its own overridden method, allowing additional behavior to be added without losing the original method's functionality.
Code Example:
class Parent {
void display() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
@Override
void display() {
super.display(); // Call the superclass's display() method
System.out.println("Child class method");
}
}
class Main {
public static void main(String[] args) {
Child child = new Child();
child.display(); // Output: Parent class method Child class method
}
}
Output:
Parent class method
Child class method
In this example, the Child class overrides the display() method from the Parent class. Inside this method, we use the super keyword to call the display() method of the Parent class before executing the additional logic in the Child class's display() method. As shown in the output, when we call the display() method from the Child class, it successfully invokes the called method and the overridden method from the superclass.
This compiles our discussion on method overriding in Java. Here are a few more topics you must explore:
- Difference Between Java And JavaScript Explained In Detail
- Advantages And Disadvantages of Java Programming Language
- Top 15+ Difference Between C++ And Java Explained! (+Similarities)
- List Of 50 Core Java Interview Questions With Answers
- 90+ Java Collections Interview Questions (+Answers) You Must Know
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Sunita Bhat 11 minutes ago
Esuru Pooja.C 2 hours ago
Ankita Das 2 days ago