Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, June 15, 2023

Top 30 C# abstract classes and interfaces interview questions and answers

What is an abstract class in C#?

An abstract class is a class that cannot be instantiated and serves as a base for deriving subclasses. It can contain abstract and non-abstract members.


How do you define an abstract class in C#?

An abstract class is defined using the `abstract` keyword before the class declaration. For example: `abstract class MyClass { }`


Can an abstract class have constructors?

Yes, an abstract class can have constructors. However, they cannot be used to create instances of the abstract class directly.


What is an interface in C#?

An interface defines a contract that a class must adhere to. It specifies a set of methods, properties, events, or indexers that a class must implement.


How do you define an interface in C#?

An interface is defined using the `interface` keyword. For example: `interface IMyInterface { }`


Can a class inherit multiple interfaces in C#?

Yes, a class can inherit multiple interfaces in C# using the `comma` (,) separator. For example: `class MyClass : IInterface1, IInterface2 { }`


Can an abstract class implement an interface?

Yes, an abstract class can implement one or more interfaces by providing the implementations for their members.


Can an abstract class be sealed in C#?

No, an abstract class cannot be sealed as it is intended to be inherited by subclasses.


What is the difference between an abstract class and an interface?

An abstract class can have both abstract and non-abstract members, whereas an interface can only have abstract members. A class can inherit only one abstract class, but it can implement multiple interfaces.


Can you create an instance of an interface in C#?

No, you cannot create an instance of an interface directly. You can only create instances of classes that implement the interface.


What is the purpose of an abstract method?

An abstract method is a method declaration without an implementation. It is meant to be overridden by the subclasses that derive from the abstract class.


Can an abstract class have non-abstract methods?

Yes, an abstract class can have both abstract and non-abstract methods. Non-abstract methods provide default behavior that can be used by the subclasses.


Can an abstract class inherit from another abstract class in C#?

Yes, an abstract class can inherit from another abstract class. In such a case, the derived class must provide implementations for all abstract members.


Can an abstract class implement an abstract method from an interface?

Yes, an abstract class can implement an abstract method from an interface. The implementing class derived from the abstract class must provide the implementation.


Can interfaces have fields in C#?

No, interfaces cannot have fields. They can only define properties, methods, events, and indexers.


Can an interface inherit from another interface?

Yes, an interface can inherit from one or more interfaces using the `colon` (:) separator. For example: `interface IMyInterface : IBaseInterface { }`


What is the purpose of an explicit interface implementation?

Explicit interface implementation is used when a class implements multiple interfaces that have members with the same name. It allows disambiguation of the members.


Can an abstract class be used to create objects?

No, an abstract class cannot be directly instantiated. You need to create a subclass and instantiate it to create objects.


Can an abstract class contain a constructor with parameters?

Yes, an abstract class can have constructors with parameters. These constructors can be used by derived classes to initialize the abstract class.


Can an abstract class have a private constructor?

Yes, an abstract class can have a private constructor. However, it can only be used by other constructors within the same abstract class.


Can an abstract class have static members?

Yes, an abstract class can have static members such as static fields, properties, methods, and events.


Can an interface inherit from a class in C#?

No, an interface cannot inherit from a class. It can only inherit from other interfaces.


Can you implement an interface explicitly and implicitly in the same class?

Yes, a class can implement an interface both explicitly and implicitly. Implicit implementation is used when the class wants to provide a public interface, while explicit implementation is used when the class wants to provide multiple interfaces or handle naming conflicts.


What is the difference between a virtual method and an abstract method?

A virtual method has an implementation in the base class and can be overridden in derived classes, whereas an abstract method has no implementation in the base class and must be overridden in derived classes.


Can an abstract class implement a virtual method?

Yes, an abstract class can implement a virtual method. However, the derived class can still override the virtual method if needed.


Can an abstract class be a sealed class in C#?

No, an abstract class cannot be sealed. The purpose of an abstract class is to provide a base for other classes to inherit from.


Can a class be both abstract and sealed in C#?

No, a class cannot be both abstract and sealed. Abstract classes are meant to be inherited, while sealed classes cannot be inherited.


Can an abstract class have a destructor?

Yes, an abstract class can have a destructor (finalizer) defined using the `~` syntax. However,

No comments:

Post a Comment