none
Derive from a class with private contructors RRS feed

  • Question

  • I have a class that has constructors private and a static method that return an object of that type
    class ClassA
    {
    	public get param1;
    	public get param2;
    	public get/set param3;
    	private ClassA(param1, param2, param3)
    	{
    	...
    	}
    	private param4;
    	private ClassA(param1, param4)
    	{
    	...
    	}
    	public static ClassA CreateClassA(param,param,...)
    	{
    	...
    	}
    }
    Is there a way to derive from this class without making the param4 public.

    Is it good design if the ClassA contains a static method that will create an object of the derived class?
    Thursday, June 25, 2009 12:36 PM

Answers

  • No duplicate code, you need only wrap functions of ClassA:
    class ClassA
    {
        public void fun1(param1, param2)
        {
            ...
            code of function fun1
            ...
        }
    }
    
    class ClassB
    {
        public void fun1(param1, param2)
        {
             _classA.fun1(param1, param2);
        }
    }
    If you want derive ClassB from ClassA you need public constructor.
    • Marked as answer by liurong luo Monday, June 29, 2009 9:33 AM
    Saturday, June 27, 2009 5:19 PM

All replies

  • Make the constructor protected, so the derived class can call it.

    If the static method in class A creates an instance of class B, the constructor of class B would have to be public or internal (assuming they're both in the same assembly). Somehow, that doesn't seem right, because the constructor of class A is private.
    I would add a static CreateClassB method in class B, and maybe add a separate factory class to decide between A and B.


    Mark the best replies as answers!
    Thursday, June 25, 2009 1:30 PM
    Moderator
  • I know that the contructors will have to be modified in protected.

    Let me explain more:

    I want to have a ClassB that will have a new param5 and two contructors like the ones in ClassA but with the extra parameter.

    In my ClassB I will have also a method CreateClassB(param,param..., param5). In this method I could call CreateClassA and based on some conditions to call a contructor or the other. But as I have mentioned I don't want to make the param4 public so I don't know the conditions.

    The reason why I would call CreateClassA is because I don't want to duplicate code in both methods.

    So my conclusion is that I can't do this without making the param4 "public". Or is the answer to my second question yes.

    Thursday, June 25, 2009 1:50 PM
  • Hi,

    You can write ClassB as wrapper for ClassA.

    class ClassA
    {
        private ClassA _classA;
    
        public param1 { get { return _classA.param1; } }
        public param2 { get { return _classA.param2; } }
        public param3
        {
            get { return _classA.param3; }
            set { _classA.param3 = value; }
        }
        private ClassB(param1, param2, param3)
        {
            ...
        }
        private param5;
        private ClassB(param1, param4)
        {
            ...
        }
        public static ClassB CreateClassB(param,param,...,param5)
        {
            _classA = CreateClassA(...);
            ...
        }
    }
    I don't see another way to resolve your problem.
    Saturday, June 27, 2009 12:56 PM
  • But you're solution propose to "overwrite" every method from classA witch is exacly what I don't want, because I could simply create classB not derived from classA and duplicate code.
    Saturday, June 27, 2009 5:10 PM
  • No duplicate code, you need only wrap functions of ClassA:
    class ClassA
    {
        public void fun1(param1, param2)
        {
            ...
            code of function fun1
            ...
        }
    }
    
    class ClassB
    {
        public void fun1(param1, param2)
        {
             _classA.fun1(param1, param2);
        }
    }
    If you want derive ClassB from ClassA you need public constructor.
    • Marked as answer by liurong luo Monday, June 29, 2009 9:33 AM
    Saturday, June 27, 2009 5:19 PM