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 10

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


da451

da451

D

Narcis

Narcis

true

rao

rao

why not C?

zerocool

zerocool

Because “The solution must allow classes to inherit from Connection.”
This is not possible, if the constructor of connection is private.

psc

psc

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.

rao

rao

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

sv1slim

sv1slim

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.

Nasreddine FAREH

Nasreddine FAREH

public class Connection
{

public static Connection create()
{

return new Connection();
}

protected Connection()
{

}
}

public class Connection2 :Connection
{

public Connection2()
{
create();

}

}

Connection cc = new Connection2();

anii

anii

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?

Chriss

Chriss

You cannot create an instance of an abstract class.