What is method overriding?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid OOP Concepts basics — a prerequisite for any developer role.
Answer
Method overriding allows a subclass to provide a specific implementation for a method that is already defined in its parent class. The subclass method has the same name, return type, and parameter list as the parent method. Rules: method must have same name and signature; return type must be the same or a covariant (subtype of parent's return type); access modifier can be same or more permissive (can't restrict access); cannot override static methods (they can be hidden, not overridden). Example: class Payment { public String processPayment(double amount) { return "Processing payment of $" + amount; } public double calculateFee(double amount) { return amount * 0.02; // 2% fee } } class CreditCardPayment extends Payment { @Override public String processPayment(double amount) { // Custom implementation double fee = calculateFee(amount); return "Processing credit card payment of $" + amount + " (Fee: $" + fee + ")"; } @Override public double calculateFee(double amount) { return amount * 0.03; // Credit cards: 3% fee } } class PayPalPayment extends Payment { @Override public String processPayment(double amount) { return "Processing PayPal payment of $" + amount; } // No override of calculateFee -- uses parent's 2% } // Polymorphism: Payment p = new CreditCardPayment(); p.processPayment(100); // Calls CreditCardPayment's version p.calculateFee(100); // Returns 3.0. super in override: call parent's original method from override: @Override public String processPayment(double amount) { String parentResult = super.processPayment(amount); return parentResult + " [Credit Card]"; }. final methods: cannot be overridden — use for security or performance. @Override annotation verifies override is correct at compile time — always use it.
Pro Tip
This topic has OOP Concepts-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is the difference between composition and inheritance?
Next
What are access modifiers?