Easy8 minType Systems
UpdatedAug 2, 2026
Edit

virtual vs override

Question Variations

  • "What is the difference between `override` and `new` (shadowing) when redefining a method in a derived class?"
  • "Can you override a method that is not marked as `virtual` or `abstract`?"
  • "What happens at runtime when you call a virtual method through a base class reference that points to a derived class instance?"
  • "Can you use the `override` keyword on a static method?"

Why This Is Asked

This is a fundamental check for understanding Polymorphism in C# and similar languages. Interviewers want to see if you know how to enable and implement method redefinition in child classes.

Key Concepts

  • virtual: A keyword used in the base class to allow a method to be redefined in a derived class.
  • override: A keyword used in the derived class to provide a new implementation of a virtual method.
  • Polymorphism: The ability for a base class reference to call the overridden method of a derived object at runtime.
  • new: The “new” keyword (method shadowing) is often contrasted with override.

Question Variations

  • “What is the difference between override and new (shadowing) when redefining a method in a derived class?”
  • “Can you override a method that is not marked as virtual or abstract?”
  • “What happens at runtime when you call a virtual method through a base class reference that points to a derived class instance?”
  • “Can you use the override keyword on a static method?”

Answers by Technology

+ Add Variant

Expected Answer (.NET 10 / C# 14)

In C#, these keywords enable Method Overriding (Polymorphism):

  • virtual: Applied to a method in the Base Class. It signals that this method can be overridden by derived classes, but it has a default implementation.
  • override: Applied to a method in the Derived Class. It provides a specific implementation that replaces the base one when called on a derived object.

Why It Matters

This allows for “Liskov Substitution”—you can treat different objects as their base type but get the specific behavior of their actual type at runtime.

Code Example

public class Animal
{
    public virtual void MakeSound() => Console.WriteLine("Generic Sound");
}

public class Dog : Animal
{
    public override void MakeSound() => Console.WriteLine("Bark!");
}

Animal myAnimal = new Dog();
myAnimal.MakeSound(); // Outputs: "Bark!" because of 'override'

Common Mistakes

  • Using new instead of override: The new keyword hides the base method (shadowing) rather than overriding it. If you call it via a base class reference, the base method will run.
  • Forgetting virtual: You cannot override a method that isn’t marked as virtual, abstract, or override.

Follow-up Questions

  • Can you override a static method? (Answer: No. Polymorphism is instance-based).
  • Difference between abstract and virtual? (Answer: abstract methods have no body and must be overridden; virtual methods have a body and may be overridden).

Expected Answer (Java 26)

Java handles method redefinition differently than C#:

  • Virtual by default: In Java, every non-static, non-private method is implicitly virtual. You don’t need a virtual keyword.
  • @Override annotation: This is not a keyword like in C#, but an annotation. It tells the compiler to check that the method actually exists in the parent class, preventing typos from creating new methods instead of overriding.
  • final: If you want to prevent a method from being virtual (disallowing overrides), you mark it as final.

Why It Matters

This default-virtual behavior is a core part of Java’s “Late Binding.” It ensures that polymorphism works out of the box without requiring explicit opt-in from the base class author.

Code Example

class Animal {
    void makeSound() { // Implicitly virtual
        System.out.println("Generic Sound");
    }
}

class Dog extends Animal {
    @Override // Optional but highly recommended
    void makeSound() {
        System.out.println("Bark!");
    }
}

Common Mistakes

  • Assuming static methods are virtual: Static methods belong to the class, not the instance, and cannot be overridden (they are “hidden”).
  • Omitting @Override: Without it, if you misspell the method name, the code will compile but polymorphism will fail silently at runtime.

Follow-up Questions

  • Can you override a constructor? (Answer: No. Constructors are not inherited).
  • What is ‘Dynamic Method Dispatch’? (Answer: The process where the JVM decides at runtime which overridden method to call based on the actual object type).

Expected Answer (PHP 8.5)

PHP handles polymorphism implicitly:

  • Implicitly Virtual: In PHP, all class methods are implicitly virtual. Any method in a parent class can be overridden by a child class unless it is marked as final.
  • parent:: keyword: To call the base class implementation from the child class.
  • Attributes: PHP does not have a native #Override attribute that enforces checks at the compiler level (like Java’s @Override), though some IDEs and static analyzers (like PHPStan) use annotations.

Why It Matters

The default-virtual behavior makes it very easy to implement the “Template Method” pattern or extend framework classes. However, it also means you must be careful to mark critical methods as final if you want to prevent them from being changed.

Code Example

class Animal {
    public function makeSound(): string {
        return "Generic Sound";
    }
}

class Dog extends Animal {
    public function makeSound(): string {
        return "Bark!"; // Overridden
    }

    public function getOriginalSound(): string {
        return parent::makeSound(); // Call base
    }
}

Common Mistakes

  • Incompatible Signatures: Overriding a method but changing the parameter types or count. PHP will throw a “Fatal Error” or “Warning” depending on the version and type of incompatibility (Liskov Substitution Principle violation).
  • Assuming private methods can be overridden: Private methods are not visible to child classes, so “overriding” them actually just creates a new, unrelated method in the child class.

Follow-up Questions

  • What is the ‘final’ keyword? (Answer: Used to prevent a class or method from being overridden or inherited).
  • Can you change the visibility when overriding? (Answer: Yes, but only to make it more visible (e.g., protected to public), not less).

Expected Answer (C++23)

In C++, a virtual member function enables runtime dispatch through a base-class pointer or reference. A derived class provides an override with the same compatible signature. Marking the derived declaration override asks the compiler to verify that it truly overrides a virtual base function, preventing accidental overloads caused by a signature mismatch.

Polymorphic base classes that may be destroyed through a base pointer need a virtual destructor so the derived destructor runs.

#include <memory>
#include <string>

class Formatter {
public:
    virtual ~Formatter() = default;
    virtual std::string format() const { return "plain"; }
};

class JsonFormatter final : public Formatter {
public:
    std::string format() const override { return "json"; }
};

int main() {
    std::unique_ptr<Formatter> formatter = std::make_unique<JsonFormatter>();
    return formatter->format() == "json" ? 0 : 1;
}

Why It Matters

Correct virtual dispatch enables safe runtime polymorphism in extensible C++ APIs. override and virtual destructors prevent subtle behavior bugs and undefined behavior during deletion through base pointers.

Common Mistakes

  • Omitting override: A signature mismatch can silently introduce a new function rather than overriding the base member.
  • Deleting a derived object through a base pointer without a virtual destructor: The derived cleanup may not run, causing undefined behavior.
  • Using inheritance where composition is simpler: Virtual hierarchies add coupling and runtime dispatch that may not be needed.

Follow-up Questions

  • What does final do? (Answer: It prevents a class from being derived or a virtual function from being overridden further.)
  • Why pass polymorphic objects by reference or pointer? (Answer: Passing by value can slice off the derived portion.)