Technical Articles

Some tricky interview questions regarding abstract classes and constructors

0 0

1. Can an abstract class have a constructor in C#?

Answer: Yes, an abstract class can have a constructor. Although you cannot instantiate an abstract class directly, the constructor is used to initialize fields in the base class when a subclass is instantiated.

Tricky Follow-up Question:

If we cannot create an instance of an abstract class, what is the purpose of having a constructor in it?

Answer: The constructor of an abstract class is invoked when a subclass object is created. It ensures that any necessary initialization defined in the abstract class happens before the subclass constructor runs.


2. If an abstract class has a constructor, how does it get called when creating an object of a derived class?

Answer: When an instance of a derived class is created, the constructor of the abstract base class is called first (using the base class constructor) to initialize fields or properties defined in the abstract class.


3. Can an abstract class constructor take parameters?

Answer: Yes, just like regular constructors, abstract class constructors can take parameters. Derived classes must call the abstract class constructor explicitly using the base keyword and pass the required parameters.

Example:


public abstract class Vehicle {
    public string Model { get; set; }

    public Vehicle(string model) {
        Model = model;
    }
}

public class Car : Vehicle {
    public Car(string model) : base(model) {
        // The constructor of the abstract class 'Vehicle' is called here.
    }
}


4. What happens if you don’t define a constructor in an abstract class?

Answer: If you don’t define a constructor, the abstract class will use the default parameterless constructor, and the derived class constructor will implicitly call this default constructor.

Tricky Follow-up Question:

What if the abstract class has fields or properties that need to be initialized but no constructor is defined?

Answer: Without a constructor, you would need to rely on default values for fields or properties. However, defining a constructor allows for more control and flexibility over the initialization of those values.


5. Can an abstract class constructor be private?

Answer: No, an abstract class constructor cannot be private because the purpose of a constructor in an abstract class is to allow the derived class to call it. However, the constructor can be protected, ensuring that only derived classes can access it.

Tricky Follow-up Question:

Why would you make an abstract class constructor protected instead of public?

Answer: Making the constructor protected ensures that only derived classes can invoke it, while preventing external classes from directly instantiating it (even if they could, which they can’t in the case of abstract classes).


6. Can you call an abstract class constructor directly from the subclass constructor?

Answer: Yes, you can and must use the base keyword in the subclass constructor to call the abstract class constructor. This is necessary if the abstract class has a parameterized constructor.

Tricky Follow-up Question:

What happens if the subclass constructor doesn’t explicitly call the base constructor?

Answer: If the abstract class has a parameterless constructor, the base constructor will be called automatically. However, if the abstract class only has parameterized constructors, the derived class must explicitly call one of those using the base keyword.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
× How can I help you?