What should you do?

You have the following code (line numbers are included for reference only):


You need to ensure that new instances of Connection can be created only by other classes by calling the Create method. The solution must allow classes to inherit from Connection.
What should you do?


A. Option A
B. Option B
C. Option C
D. Option D

microsoft-exams

11 thoughts on “What should you do?

  1. Answer B is incorrect: You can not create an instance of static class.

    Correct answer is D:
    public class Connection
    {
    protected Connection() { }

    public static Connection Create()
    {
    return new Connection();
    }
    }

    public class Connection02 : Connection
    {
    protected Connection02() { }

    public static new Connection02 Create()
    {
    return new Connection02();
    }
    }

    class Test000
    {
    static void Main(string[] args)
    {
    Connection myTest01 = Connection.Create();
    Connection02 myTest02 = Connection02.Create();
    }
    }

    1. Please see PDUB’s explanation. Option C will not allow you to inherit from Connection; I’ve tried this in Visual Studio. A derived class MUST be able to call a constructor of the base class. Making the base class constructor private would prevent this.

  2. The answer is D, protected access modifier will allow the class to be inherited and the protection level of protected doesn’t allow creating a new instance of that class but allows the Connection class to call the Create() method from within the class. ex. creating and instance of the inheriting class to invoke the method.

    Setting the access modifier to private wouldn’t allow another class to inherit from it.

    1. because, Class will not be initiated by other class by making use of its constructor. SO outside classes have to call static Create method. The static create method will be able to call private constructor because they are in same class.

      1. But “The solution must allow classes to inherit from Connection.”
        So constructor need to be protected -> Answer D

  3. Nothing is correct.
    B is incorrect because other class should be able to inherit from Connection. A static class i seald, i.e. you can’t inherit from it which was a requirement

  4. Both of you are incorrect the solution requires not only inheritance, but that no classes can use it constructor including derived ones. Therefore, C is correct by marking the constructor private.

  5. if you choose A, you can not use new () operator in order to create an instance of Connection;
    If you choose B or even C, you can not fulfill the requirement:
    “The solution must allow classes to inherit from Connection”,
    As everybody knows, private constructors or even static classes will block up inheritance behavio;
    So, remains D.

    Marcus Vinicius Matias de Arruda.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.