OOP (object oriented programming)What is the class?What do you mean by object?What are the differences between class and object?Can you create an object without using new operator in C#?What is constructor and how many constructors can have one class?Differences between constructor and method of the class? What is default constructor?What is parameterized Constructor in C#?What is private constructor: In what instances you will declare a constructor to be private?What is static constructor, Is it possible to have a static constructor in class. If yes why we need to have a static constructor?Does C# provide copy constructor for an object? How do you call the multiple constructors of a class with single object creation?What is constructor chaining in C#?Can a constructor be called directly from a method?What is constructor overloading and how it’s different than method overloading?What is the difference between constructor overloading and method overloading?Is it possible to overload copy constructor in C#?Can we overload static constructors in C#?Can we overload private constructors in C#?Can we give return type of the constructor in C#?What is the destructor and when it’s called?Is it possible to call constructor and destructor explicitly?What is the Structure and why we need it although we have a class?What are the similarities between Class and Structure?What is the difference between Class and Structure?What is copy structure?What is nested structure?Is it always necessary to create an object of the class?How many different ways to create an object of the class?What are the pros and cons of creating object by new() keyword?What are the pros and cons of delegate object creation to DI container?What are the pros and cons of creating an object by reflection?What are the pros and cons of getting an object from an object pool?What are the pros and cons of creating an object by deserialization?Is it possible to create an object without a class in C#?What is constant?What is static modifier? What are the Static fields and methodsWhat is Static ReadOnly?What are the limitations of static?What is readonly? What’s the difference between constant and read-only?What is this keyword?What is base keyword?What is the difference between this and base keyword?Can “this” keyword be used within static method?What are the accessors?What is the static class? Why we need of static class?If someone wants to create static class then what are the rules for the static class?What are the limitations of using static keyword?What are finalizers in c#?How to create N number of instances of C# class?What are the Nested Classes and why we use them?What are the basic four pillars of OOP?What is the Inheritance and why we need of inheritance?How do you inherit a class into other class in C#?What is the concept of base and derive class?What are the different types of inheritance?We have two classes’ base class and child class. A is the base class and B is the child class, If we create an instance of child class then which class’s constructor called first?Does a derived class can inherit the constructors of its base class?What should we do that if we create an object of child class then the parameterized constructor of base class must be invoked?As we know that base constructor invoked first when we create instance of child class but if we create an instance of child class by parameterized constructor and base class has both default and parameterized constructor then which constructor of the base will be invoked?Can you assign an object of derived class to the variable of base class and if both have the same method name then which will be invoked?Can we create instance of base class and store it to derive class?Can we create derive class object inside base class, and if create instance of child class then what will happen?Can we inherit child class from 2 base classes? if yes then how? If not then why?Does C# support Multiple Inheritance?Why multiple inheritance is not supported in C# and why it’s supported in C++?How is multiple inheritance achieved in C#?What are Access Modifiers? Explain private, public, protected, internal, protected internal access modifiersWhat are the default access modifiers of the class?Why classes cannot be declared as protected?Can we declare private class in namespace?What are the valid access specifier used for the declaration of class at namespace level? If we inherit a class, do the private variables also get inherited?Can you prevent your class from being inherited?Can you prevent your class from being inherited without using sealed keyword?What is abstraction?What is encapsulation?What is the difference between abstraction and encapsulation?What is polymorphism?What is static or compile time polymorphism?What is runtime polymorphism or late binding or dynamic binding?What is method overloading?When and why we should use overload methods?What is inheritance based overloading?What are the advantages of using overloading?Can we overload the method in the same class?What is the execution control flow in overloaded methods?What is method overriding?What s virtual keyword?What are the key points to make the method as overridden?When it is must to override the method?When a derived class can overrides the base class member?Can we declare fields inside the class as virtual?When we treat sub-class method as an overriding method?Can we override private virtual method in c#?Can we override method in the same class?Can we execute parent class method if it is overridden in the child class?If we have virtual in base class and the same method is overridden in child class, by creating instance of child class and assign it to base class, then which of the method will be invoked first.What is the difference between method overloading and method overriding?What is method hiding?Can you access a hidden method in the derived which is declared in the base class?What is the difference between method overriding and method hiding?You have a component with 2 parameters and deployed to client side, now you have changed your method with 3 parameters, how can you deploy this without affecting the client code?What is operator overloading?What is abstract class and why we need of it?What are the rules of abstract classes?What is an abstract method?What is concrete method?When do you use abstract class in C#?When to use the abstract method in C#?

What is method overriding?

Method overriding is a concept in object-oriented programming that allows a subclass to provide a specific implementation for a method that is already defined in its superclass. When a subclass overrides a method from its superclass, it replaces the inherited implementation with its own implementation while keeping the method signature (name, return type, and parameter types) the same.

Key points about method overriding:

  1. Inheritance Required: Method overriding is only applicable in inheritance hierarchies, where a subclass inherits methods from its superclass.
  2. Method Signature: The overridden method in the subclass must have the exact same method signature (name, return type, and parameter types) as the method in the superclass.
  3. Keyword: In most object-oriented languages like Java and C#, you use the override keyword to indicate that a method in the subclass is intended to 'override' a method in the superclass.
  4. Polymorphism: Method overriding is a key aspect of polymorphism, allowing objects of the subclass to be treated as objects of the superclass while ensuring that the appropriate overridden method is called at runtime.
  5. Access Modifiers: The access modifier of the overriding method in the subclass can't be more restrictive than the access modifier of the method in the superclass. It can be equally or more permissive (public > protected > internal/default).
  6. Method Body: The overridden method in the subclass provides a new implementation specific to the subclass's behavior. It can't have a different number of parameters or different types of parameters compared to the method in the superclass.
  7. base Keyword: In some languages (like C#), you can use the 'base' keyword to explicitly call the overridden method of the parent class from within the overridden method of the child class.
  8. Runtime Polymorphism: When a method is called through a reference of the base class that actually points to an object of the derived class, the overridden method in the derived class is executed at runtime.

Method overriding allows for a more specialized behavior in subclasses, promoting code extensibility and reusability. It ensures that the most appropriate method implementation is used based on the actual type of the object, providing dynamic behavior and facilitating the principle of "substitutability" in object-oriented design.

Let's illustrate method overriding in C# using an example involving a base class Vehicle and two derived classes Car and Bicycle. We'll override a method called StartEngine() to provide specific implementations for each type of vehicle.


using System;

class Vehicle
{
    public virtual void StartEngine()
    {
        Console.WriteLine("Starting the engine of a generic vehicle");
    }
}

class Car : Vehicle
{
    public override void StartEngine()
    {
        Console.WriteLine("Starting the engine of a car");
    }
}

class Bicycle : Vehicle
{
    public override void StartEngine()
    {
        Console.WriteLine("Bicycles don't have engines to start");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Vehicle vehicle1 = new Vehicle();
        Vehicle vehicle2 = new Car();
        Vehicle vehicle3 = new Bicycle();

        vehicle1.StartEngine();
        vehicle2.StartEngine();
        vehicle3.StartEngine();
    }
}
In this example:
  1. The 'Vehicle' class defines a 'virtual' method 'StartEngine()' with a default implementation for a generic vehicle.
  2. The 'Car' class inherits from 'Vehicle' and overrides the 'StartEngine()' method with a specific implementation for starting a car's engine.
  3. The 'Bicycle' class also inherits from Vehicle and overrides the 'StartEngine()' method to provide a message stating that bicycles don't have engines.
  4. In the 'Main' method, instances of 'Vehicle', 'Car', and 'Bicycle' are created.
  5. When the StartEngine()' method is called on each object, the appropriate overridden version is executed based on the actual type of the object.

When you run the program, you'll see the following output:


Starting the engine of a generic vehicle
Starting the engine of a car
Bicycles don't have engines to start

Despite using the same method name, the appropriate version of the method is executed based on the actual type of the object at runtime. This demonstrates method overriding and dynamic behavior, which is a key characteristic of polymorphism.