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?
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
D
http://www.aiotestking.com/microsoft/you-need-to-ensure-that-new-instances-of-connection-can-be-created-only-by-other-classes-by-calling-the-create-method/
Agree with you
Make the Constructor private.Then the only way to to get an instance of Connection is to call the public static Create method.
So the answer is C
If the constructor is private, then the class cannot be inherited due its contructor is visible only inside itself.
Sorry.. The other part of the question is that Connection class should be inheritable.. So make the constructor “protected”
So the answer is D
Definitly A
Agree. Definitly !
The question says we want an instantiation of the class.
Abstract means that the class cannot be instantiated. The class can only be a base class.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract
So definitely not A.
Agree with you
A
d
It’s D. Tested in console app. A is wrong cuz you won’t be able to “return new Connection()” from abstract class.
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
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.
Sorry the correct answer is D.
D
it is correct , You cannot create an instance of an abstract class or static class. so the answer is the answer is D
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.
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.
D , it says derive from any other classes.