PrepAway - Latest Free Exam Questions & Answers

You need to ensure that new instances of Connection can…

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 theCreate method. The solution must allow classes to inherit from Connection.
What should you do?

PrepAway - Latest Free Exam Questions & Answers

A.
Option A

B.
Option B

C.
Option C

D.
Option D

Explanation:
The following list provides the main features of a static class:
* Contains only static members.
* Cannot be instantiated.
* Is sealed.
* Cannot contain Instance Constructors.
Creating a static class is therefore basically the same as creating a class that contains only static members and
a private constructor. A private constructor prevents the class from being instantiated.
Incorrect:
Not A: An abstract method is a method that is declared without an implementation.
Not C: Private methods can be called from derived classes.
Static Classes and Static Class Members (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

22 Comments on “You need to ensure that new instances of Connection can…

    1. JS1985 says:

      protected means that the Create() method canonly called by Connection-derived classes.
      The question is that ‘other’ classes (any class, not only derived ones) must be able to call the Create() method.

      Static means that you don’t need an instantiation of the class: you can invoke its public methods directly, so the correct answer is:

      B




      2



      5
  1. Seamus says:

    The possible answer is making connection class abstract.
    So definitely A.
    Making class static makes the connection class sealed and cannot be interited. According the question,the class should be inheritable..So not B

    Setting the constructor to private prevents the Connection from being inherited. So not C.

    By making the method protected, it can only be accessible from the classes derived from Connection. So not D.

    Please correct me if I am wrong.




    1



    2
  2. Alex says:

    Hello everyone!

    It must be C.

    Explanation: well, we’ve got two requirements to match:
    – Requirement 1) “ensure that new instances of Connection can be created only by other classes by calling the Create method”
    – Requirement 2) “The solution must allow classes to inherit from Connection.”

    A is wrong because it doesn’t prevent instantiation in another classes by calling a default parameterless constructor. So it violates Requirement 1
    B violates Requirement 2 (tested by code – static class is automatically made sealed) and violates Requirement 1 as well!
    C is correct, it matches both Requirements
    D is wrong because it violates Requirement 1, since it allows instantiation in inherited classes by using the protected constructor, not only the Create method.

    This is a good question to check the understanding of inheritance and of the instantiation of classes using constructors and factory methods.




    3



    0
    1. didii says:

      Your explanations aren’t completely correct. Here is my take.

      Both answer A and B will generate a compiler error on instantiating a Connection class: both a static and abstract class cannot be instantiated and thus the code new Connection() will fail. This has nothing to do with requirements, this is about valid C# code.

      C will not pass the requirement of the derived classes since they will not be able to call their parents constructor (which is mandatory for any derived class).

      D is the only one that will compile but it will not pass the requirement that Connection can only be instantiated using the Create() method since derived classes will still have access to its constructor.

      So in fact, no answer is completely correct. However D violates the least requirements.




      0



      0
  3. martin says:

    public class Connection
    {
    public static Connection Create()
    {
    return new Connection();
    }
    protected Connection() { } // D is the correct answer!
    }

    class Connection2 : Connection // Inheritance Works
    {
    }

    static void Main(string[] args)
    {
    Connection myConnection;
    myConnection = Connection.Create(); // Create works
    myConnection = new Connection(); // does not work (due to the protected constructor)
    }




    5



    0

Leave a Reply