Explain method overloading and method overriding in Java

๐Ÿ’ก Concept: Method Overloading and Overriding

Method overloading allows multiple methods in the same class with the same name but different parameters. Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass.

๐Ÿ“˜ Quick Intro

Overloading is compile-time polymorphism; overriding is runtime polymorphism, essential for flexibility and code reuse.

๐Ÿง  Analogy

Overloading is like having multiple phone numbers for different purposes; overriding is like answering the phone differently depending on who is calling.

๐Ÿ”ง Technical Explanation

  • Method overloading: Same method name, different parameter list within a class.
  • Method overriding: Subclass method with same signature as superclass method.
  • Overloading happens at compile-time; overriding at runtime.
  • Overriding requires inheritance.
  • Overriding supports dynamic method dispatch and polymorphism.

๐ŸŽฏ Use Cases

  • โœ… Use overloading to increase method flexibility.
  • โœ… Use overriding to change or extend behavior in subclasses.
  • โœ… Essential for designing polymorphic systems.

๐Ÿ’ป Java Code Example


class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }
}

class Animal {
    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(2,3));     // 5
        System.out.println(calc.add(2,3,4));   // 9

        Animal a = new Dog();
        a.sound();  // Dog barks
    }
}

โ“ Interview Q&A

Q1: What is method overloading?
A: Multiple methods with same name but different parameters.

Q2: What is method overriding?
A: Subclass provides specific implementation of superclass method.

Q3: Which polymorphism is overloading?
A: Compile-time polymorphism.

Q4: Which polymorphism is overriding?
A: Runtime polymorphism.

Q5: Does overriding require inheritance?
A: Yes.

Q6: Can private methods be overridden?
A: No.

Q7: Can static methods be overridden?
A: No, they are hidden not overridden.

Q8: What is dynamic method dispatch?
A: Runtime decision to invoke overridden method.

Q9: Can constructors be overloaded?
A: Yes.

Q10: Can method overloading occur across classes?
A: No, only within the same class.

๐Ÿ“ MCQs

Q1. What is method overloading?

  • Different method names
  • Multiple methods with same name but different parameters
  • Abstract methods
  • Static methods

Q2. What is method overriding?

  • Overloading
  • Subclass implementation of superclass method
  • Interface implementation
  • Constructor overloading

Q3. Which polymorphism is compile-time?

  • Method overriding
  • Method overloading
  • Dynamic dispatch
  • Inheritance

Q4. Which polymorphism is runtime?

  • Method overloading
  • Method overriding
  • Static dispatch
  • Dynamic typing

Q5. Does overriding require inheritance?

  • No
  • Yes
  • Sometimes
  • Never

Q6. Can private methods be overridden?

  • Yes
  • No
  • Sometimes
  • Only public

Q7. Can static methods be overridden?

  • Yes
  • No
  • Sometimes
  • Depends

Q8. What is dynamic method dispatch?

  • Compile-time binding
  • Runtime decision to invoke overridden method
  • Static dispatch
  • Method hiding

Q9. Can constructors be overloaded?

  • No
  • Yes
  • Sometimes
  • Never

Q10. Can method overloading occur across classes?

  • Yes
  • No
  • Sometimes
  • Only with inheritance

๐Ÿ’ก Bonus Insight

Understanding overloading and overriding is crucial for mastering Java polymorphism and writing flexible code.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: