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

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?

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

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



Leave a Reply 9

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

9 + fourteen =


Ravi

Ravi

A

Heitor

Heitor

no, because you cannot instantiate an abstract class.

Sbuse

Sbuse

D – The solution should allow inheritance. Static classes cannot be inherited.

Yoelvis

Yoelvis

C
with the private constructor you disallow the class to be constructed with the default constructor so you will need to call that Create method.

Heitor

Heitor

you are right about the private constructor, but C is wrong because of the inheritance detail:

The solution must allow classes to inherit from Connection.

by using a protected constructor you allow inherited classes to have a parameterless ctor

the right answer is D

Najlepszy Programista Swiata DAGO

Najlepszy Programista Swiata DAGO

D

harsh

harsh

Yes answer should be D
A–>you get compile error at 05: can not create an instance of abstract class
B–>static classes can not be inherited
C–> class is inaccessible due to its protection level

Lord Vader

Lord Vader

answer is D.
The keyword “protects” the class from having its’ constructor called by external classes. However unlike the private keyword, protected will allow derived classes to access the class member.

So what good is it? Classes that use it will employ other means to create instances of the class. A static “CreateInstance” method within the class can call the protected constructor and return an instance.