Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, May 11, 2023

C# Interview Questions on Inheritance: Exploring Class Hierarchy and Code Reusability

What is inheritance in C#?

Inheritance is a mechanism in C# that allows a class to inherit properties, methods, and fields from another class, known as the base or parent class. The class that inherits these members is called the derived or child class.

How do you declare a class that inherits from another class in C#?

To declare a class that inherits from another class in C#, you use the colon (:) symbol, followed by the name of the base class. Here's an example:

class ChildClass : ParentClass

{

// ChildClass members

}

What are the types of inheritance in C#?

There are five types of inheritance in C#:

1. Single inheritance: A derived class inherits from a single base class.
2. Multi-level inheritance: A derived class inherits from a base class, which itself inherits from another base class.
3. Hierarchical inheritance: Multiple derived classes inherit from a single base class.
4. Multiple inheritance: A derived class inherits from multiple base classes.
5. Hybrid inheritance: A combination of any of the above types.


Can you give an example of single inheritance in C#?

Sure. Here's an example:

class Animal

{

public void Eat()

{

Console.WriteLine("The animal is eating.");

}

}

class Dog : Animal

{

public void Bark()

{

Console.WriteLine("The dog is barking.");

}

}

class Program

{

static void Main(string[] args)

{

Dog myDog = new Dog();

myDog.Eat();

myDog.Bark();

}

}

In this example, the Animal class is the base class and the Dog class is the derived class. The Dog class inherits the Eat() method from the Animal class and also defines its own Bark() method. The Main() method creates an instance of the Dog class and calls both the Eat() and Bark() methods.

What is the difference between private and protected inheritance in C#?

Private inheritance means that the derived class can only access the public and protected members of the base class through public methods of the derived class. Protected inheritance means that the derived class can access the protected members of the base class directly.

What is method overriding in C#?

Method overriding is a feature of inheritance in C# that allows a derived class to provide its own implementation of a method that is already defined in the base class. To override a method, the derived class must use the override keyword and provide its own implementation of the method.

Can you give an example of method overriding in C#?

Yes, here's an example:

class Animal

{

public virtual void MakeSound()

{

Console.WriteLine("The animal makes a sound.");

}

}

class Dog : Animal

{

public override void MakeSound()

{

Console.WriteLine("The dog barks.");

}

}

class Program

{

static void Main(string[] args)

{

Animal myAnimal = new Animal();

myAnimal.MakeSound();



Dog myDog = new Dog();

myDog.MakeSound();

}

}

In this example, the Animal class defines a virtual MakeSound() method, which is overridden in the Dog class using the override keyword. The Main() method creates instances of both the Animal and Dog classes and calls the MakeSound() method on each of them. When the MakeSound() method is called on the Animal object, it outputs "The animal makes a sound." However, when the same method is called on the Dog object, it outputs "The dog barks." because the MakeSound() method is overridden in the Dog class.

Can you override a non-virtual method in C#?

No, you cannot override a non-virtual method in C#. To allow a method to be overridden, it must be marked as virtual or abstract in the base class.

Can a class inherit from multiple base classes in C#?

No, C# does not support multiple inheritance of classes. However, a class can implement multiple interfaces, which provide a similar functionality.

Here's an example code that demonstrates inheritance in C#:

using System;

class Animal

{

public void Eat()

{

Console.WriteLine("The animal is eating.");

}

public virtual void Move()

{

Console.WriteLine("The animal is moving.");

}

}

class Dog : Animal

{

public void Bark()

{

Console.WriteLine("The dog is barking.");

}

public override void Move()

{

Console.WriteLine("The dog is running.");

}

}

class Program

{

static void Main(string[] args)

{

Dog myDog = new Dog();

myDog.Eat();

myDog.Move();

myDog.Bark();

}

}

In this example, we have a base class called Animal, which defines a method to eat and a virtual method to move. We also have a derived class called Dog, which inherits from Animal and defines a method to bark and overrides the move method.

In the Main method, we create a new Dog object and call its methods to eat, move, and bark. The output of the program is:

The animal is eating.

The dog is running.

The dog is barking.

What is the purpose of the sealed keyword in C#? 

 The sealed keyword in C# is used to restrict inheritance and prevent a class or method from being overridden or further inherited by other classes. When a class is marked as sealed, it cannot serve as a base class for any other class. Similarly, when a method is marked as sealed, it cannot be overridden in any derived class.

How does the sealed keyword relate to polymorphism in C#? 

Polymorphism is a fundamental concept in object-oriented programming that allows objects of different types to be treated as instances of a common base class. In C#, polymorphism is achieved through inheritance and method overriding. The sealed keyword is used to restrict polymorphism by preventing further inheritance and method overriding. By marking a class or method as sealed, you prevent any further modification or extension of that class or method, ensuring that the behavior remains consistent across all derived classes.


 Can you provide an example of using the sealed keyword in C#? 

Certainly! Here's an example that demonstrates the use of the sealed keyword in C#:

class Vehicle
{
    public virtual void Start()
    {
        Console.WriteLine("Vehicle started.");
    }
}

class Car : Vehicle
{
    public sealed override void Start()
    {
        Console.WriteLine("Car started.");
    }
}

class SportsCar : Car
{
    // This line will cause a compilation error since the 'Start' method in 'Car' is sealed.
    // public override void Start() { }
}

class Program
{
    static void Main(string[] args)
    {
        Vehicle vehicle = new Vehicle();
        vehicle.Start();  // Output: Vehicle started.

        Car car = new Car();
        car.Start();      // Output: Car started.

        SportsCar sportsCar = new SportsCar();
        sportsCar.Start(); // Compilation error: 'Start' is sealed in 'Car'.

        Console.ReadLine();
    }
}


In the example above, the Vehicle class defines a virtual Start method that can be overridden. The Car class derives from Vehicle and seals the Start method using the sealed keyword. This means that the Car class cannot be further inherited and its Start method cannot be overridden. Attempting to override the Start method in the SportsCar class leads to a compilation error because it violates the seal set in the Car class.

No comments:

Post a Comment