PrepAway - Latest Free Exam Questions & Answers

You need to ensure that new instances of Connection can be created only by other classes by calling the Create

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?

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.

Static Classes and Static Class Members (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

10 Comments on “You need to ensure that new instances of Connection can be created only by other classes by calling the Create

  1. psc says:

    If you make the class static you cannot inherit from it. A static lass is sealed. But the requirement is: “The solution must allow classes to inherit from Connection”. So the answer cannot be option B.




    2



    0
  2. rao says:

    Ok, Ok… I got it!
    The Connection class must allow other classes to inherit from it. SO…
    The Connection class can not be static
    The Connection class can not have private constructor
    The connection class can not be abstract (need to have at least one abstract method)

    So the correct answer is protected!!! D




    2



    0
    1. sv1slim says:

      right it’s D: “protected Connection() { }” this is a default constructor which changed from public to protected, which means you will not be able to initialize: Connection con = new Connection(); from another class.




      0



      0
  3. Nasreddine FAREH says:

    public class Connection
    {

    public static Connection create()
    {

    return new Connection();
    }

    protected Connection()
    {

    }
    }

    public class Connection2 :Connection
    {

    public Connection2()
    {
    create();

    }

    }

    Connection cc = new Connection2();




    0



    0
  4. anii says:

    Ok, I have a question. Why not A?

    Instruction says “You need to ensure that new instances of Connection can be created ONLY by OTHER classes by calling the Create method”. Isn’t the abstract modifier that does just that?

    So the abstract class serves only as a base class for other classes (that derive from this abstract class). https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/abstract

    Is there something I’m missing here?




    0



    0

Leave a Reply