What is Method Overriding in C#?

πŸ’‘ Concept: Method Overriding

Method overriding allows a derived class to provide its specific implementation for a method that is already defined in its base class using the virtual and override keywords.

πŸ“˜ Quick Intro

Method overriding in C# supports runtime polymorphism. It lets a child class override the behavior of a base class method marked with virtual using the override keyword.

🧠 Analogy

Think of a base class method like a blueprint. A derived class can customize the blueprint to build something different while still following the original structure. That’s overriding in actionβ€”changing how something behaves without changing the definition.

πŸ”§ Technical Explanation

  • Only virtual methods can be overridden using override in derived classes.
  • The base method must be marked as virtual, abstract, or override.
  • Overriding enables dynamic dispatch or runtime polymorphism.
  • Signatures must match between base and overridden method.
  • Optional: base.MethodName() can be used to call the base implementation.

🎯 Use Cases

  • Customizing behavior in derived classes while preserving a consistent interface.
  • Implementing polymorphic behavior in frameworks or APIs.
  • Allowing extensibility in large-scale object-oriented systems.
  • Using ToString(), Equals(), or other virtual base class methods.

πŸ’» Code Example

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Animal speaks");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Dog barks");
    }
}

// Usage
Animal pet = new Dog();
pet.Speak(); // Output: Dog barks

❓ Interview Q&A

Q1: What is method overriding?
A: It allows a derived class to redefine a base class method marked as virtual.

Q2: What keyword is used to override a method?
A: The override keyword.

Q3: Can we override a non-virtual method?
A: No, only virtual or abstract methods can be overridden.

Q4: What is runtime polymorphism?
A: The method call is resolved at runtime based on the object’s type.

Q5: Can you override static methods?
A: No, static methods cannot be overridden.

Q6: Can constructors be overridden?
A: No, constructors are not inherited and cannot be overridden.

Q7: Can access modifiers differ between base and override methods?
A: Yes, but the overriding method cannot have a more restrictive access level.

Q8: Can a method be both overloaded and overridden?
A: Yes, as long as their signatures differ.

Q9: What is the use of base.Method()?
A: To call the base class implementation from the derived class.

Q10: Can override be used with abstract methods?
A: Yes, abstract methods must be overridden in derived classes.

πŸ“ MCQs

Q1. Which keyword is used to override a method?

  • virtual
  • new
  • override
  • base

Q2. What enables method overriding?

  • Static method
  • Virtual method in base class
  • Private method
  • Constructor

Q3. Can you override static methods?

  • Yes
  • No
  • Only if sealed
  • If protected

Q4. What is polymorphism?

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

Q5. Is overriding decided at runtime?

  • No
  • Yes
  • Compile-time
  • Depends on CLR

Q6. What happens if you forget override keyword?

  • Compiles as override
  • Runtime error
  • Hides base method (not overrides)
  • Calls constructor

Q7. Which access modifier allows overriding?

  • Private
  • Internal
  • Protected internal
  • Public or protected

Q8. Can you override a sealed method?

  • Yes
  • No
  • Only in base
  • Only in derived

Q9. Can you override an abstract method?

  • No
  • Yes
  • Only if sealed
  • Only if internal

Q10. How do you call the base method?

  • super()
  • base.MethodName()
  • this.Method()
  • None

πŸ’‘ Bonus Insight

Method overriding brings flexibility and reusability to object-oriented design. Proper use of polymorphism allows extending applications without rewriting existing logic, making your codebase more scalable.

πŸ“„ PDF Download

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

πŸ” Navigation

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup