How do you make a class not instantiable

A) Making class as Abstract
B) Having a Private constructor
C) Making the class sealed
D) Both A & B

Showing Answers 1 - 75 of 95 Answers

samiksc

  • Jan 9th, 2006
 

Only choice A is correct.

B is NOT correct since a class with a private constructor can be instantiated through a static member function of the class. Try out the following code:

class PrivateConstructor{

      private PrivateConstructor() {

               Console.WriteLine("Instantiating private constructor");

}

public static void CreateObj()

{

PrivateConstructor p = new PrivateConstructor();

}

}

samiksc

  • Jan 9th, 2006
 

Only choice A is correct.

B is NOT correct since a class with a private constructor can be instantiated through a static member function of the class. Try out the following code:

class PrivateConstructor{

      private PrivateConstructor() {

               Console.WriteLine("Instantiating private constructor");

      }

      public static void CreateObj() {

              PrivateConstructor p = new PrivateConstructor();

     }

}

Call PrivateConstructor.CreateObj() from Main(). It outputs "Instantiating private constructor"

  Was this answer useful?  Yes

anand

  • Jan 30th, 2006
 

A and B Both

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

}

private Class1()

{

}

}

public class class2

{

public class2()

{

Class1 cl = new Class1();

}

}

Error Got is:-

class1:class1() is inaccessible due to protection level

  Was this answer useful?  Yes

s

  • Feb 15th, 2006
 

only A ics correct because jus saying tht a class has a private constructor does not mean that it cant have any static methods which can be used to instantiate the classs. the option B is incomplete to be called true.

  Was this answer useful?  Yes

Mayur

  • Mar 9th, 2006
 

To making a Class Sealed

  Was this answer useful?  Yes

Rajendra Prasad.k

  • Mar 14th, 2006
 

Both A and B

  Was this answer useful?  Yes

toyash

  • Mar 23rd, 2006
 

Ans : D ( Both A & B).

when you are trying to instanciate the class which has private constructor , you will get the follwoing error.

ConsoleApplication1.PrivateConst.PrivateConst()' is inaccessible due to its protection level

  Was this answer useful?  Yes

Yazdani

  • Apr 15th, 2006
 

This is reply to Sameekha (She is right)

Answer is : A

If we are declaring a Constructor as Private we cannot instatiate it Directly.

Here all the methods should be declared as static so that you can instantiate and call a methods directly

ClassName.StaticMethodName();

Currect me if i am wrong

thanks

  Was this answer useful?  Yes

sean

  • Apr 20th, 2006
 

Ans: A&B

You can call static method does not means the a class are instantiable.

Static memebers are part of class, while instnace memeber are associated with the instances of a type. In C#, it is not legal to access a static method or member variable through instance. By example

class TestStatic{

static int num;

}

TestStatic ts = new TestStatic();

Console.WriteLine(ts.num);

'Exercise.TestStatic.num' is inaccessible due to its protection level

  Was this answer useful?  Yes

JagadeeshM

  • Apr 25th, 2006
 

Even after having the constructor as private a class can be instantiated and can access non static members...

check the following code ...

using System;

class PrivateConstructor{

    private PrivateConstructor()
 {
         Console.WriteLine("Instantiating private constructor");
 }

 public static PrivateConstructor CreateObj()
 {
  PrivateConstructor p = new PrivateConstructor();
  return p;
 }
 public void message()
 {
  Console.WriteLine("Calling non static member..");
 }

}
class createObjClass
{
 public static void Main()
 {
  PrivateConstructor pc = PrivateConstructor.CreateObj();
  pc.message();
 }
}

Would like to know the comments on this...

Thanks

  Was this answer useful?  Yes

HI,

Only Choice is A correct as if we declare constructor private still we can access that constructor using some member function of the class. Object can still be created.

  Was this answer useful?  Yes

Guest

  • Jul 16th, 2006
 

we can prevent the class from interting by using keyword sealed

  Was this answer useful?  Yes

Anantharaman

  • May 28th, 2013
 

D) Both A & B

  Was this answer useful?  Yes

kunal

  • Jun 12th, 2013
 

A and B

  Was this answer useful?  Yes

Chandrashekhar

  • Nov 16th, 2013
 

static class FEATURE are:

They only contain static members.
They cannot be instantiated.
They are sealed.
They cannot contain Instance Constructors
a static class is that the compiler can check to make sure that no instance members are accidentally added.

The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor

  Was this answer useful?  Yes

Pankaj

  • May 6th, 2014
 

b. By making constructor private,we can not instantiate the class.

  Was this answer useful?  Yes

Jimuta Bahan Sahu

  • May 27th, 2014
 

C. If we make the class sealed.

Code
  1.  sealed class A

  2.     {

  3.         A()

  4.         {

  5.         }

  6.     }

  Was this answer useful?  Yes

Huy

  • Jul 15th, 2014
 

Simply declare the class using the abstract keyword. As oppose to a regular (concrete class), abstract classes are not instantiated but are used to derived other subclasses.

  Was this answer useful?  Yes

BoYuan

  • Jul 30th, 2014
 

The answer is A.

For B, we cannot instantiate the class if there is only one constructor and that only constructor is private, otherwise, if there is at least one constructor is public, then that class is instantiatable.

For C, keyword "seal" only means this class cannot have sub-classes, but we can still instantiate it.

  Was this answer useful?  Yes

Sunil

  • Sep 24th, 2014
 

a)making class as abstract
is Correct answer.

  Was this answer useful?  Yes

Sushmit

  • Oct 7th, 2014
 

a & b

  Was this answer useful?  Yes

priya

  • Nov 6th, 2014
 

a

  Was this answer useful?  Yes

Aditya

  • Nov 8th, 2014
 

C

  Was this answer useful?  Yes

suman

  • Nov 12th, 2014
 

C) Making the class sealed

  Was this answer useful?  Yes

Ajay

  • Nov 16th, 2014
 

B) Both A and B

  Was this answer useful?  Yes

Manoj Kumar

  • Nov 20th, 2014
 

C

  Was this answer useful?  Yes

swaroopa

  • Dec 3rd, 2014
 

A)Making class as Abstract

  Was this answer useful?  Yes

veera

  • Dec 11th, 2014
 

D

  Was this answer useful?  Yes

Mike Murphy

  • Dec 11th, 2014
 

a

  Was this answer useful?  Yes

Suraj

  • Jan 17th, 2015
 

D

  Was this answer useful?  Yes

S.M. Sadrul Islam Asif

  • Feb 5th, 2015
 

Code
  1. public final class Name {

  2.     private Name() {

  3.         throw new RuntimeException();

  4.     }

  5. }

  Was this answer useful?  Yes

Chinmay

  • Mar 22nd, 2015
 

By declaring it static.

  Was this answer useful?  Yes

shashank

  • Apr 15th, 2015
 

D) Both A&B

  Was this answer useful?  Yes

ramya

  • Oct 13th, 2015
 

Making class as Abstract

  Was this answer useful?  Yes

Naveen Kumar Shivanadri

  • Sep 8th, 2016
 

Answer is A is always correct and B is also correct only the situation when the class does not have the static methods on it (in the question, there is no mentioned that there is no static method in it)
So, only A is correct answer.

Naveen Kumar Shivanadr.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions