Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to derive from a class with an internal constructor?

I'm working with a 3rd party c# class that has lots of great methods and properties - but as time has gone by I need to extend that class with methods and properties of my own. If it was my code I would just use that class as my base class and add my own properties and method on top - but this class has an internal constructor. (In my opinion it was short sited to make the constructor internal in the first place - why limit the ability to subclass?)

The only thing I could think of was to create method / properties on my class that simply called into theirs - but it's acres of code and, well, it just doesn't "feel" right.

Is there any way to use this class a base class?

like image 452
will Avatar asked Jun 17 '09 04:06

will


People also ask

Can a constructor be internal?

A constructor can be either internal or public, an internal constructor marks contract as abstract.

Can a constructor be inherited by a derived class from the base class?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.

When would you use an internal constructor?

internal constructor are good when you don't want the client to create the instance. You'd usually want to do it if you want to control how the instance should be created.

Can we declare private constructor in derived class?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class.


3 Answers

You ask: "Why limit the ability to subclass?"

Because designing for inheritance is tricky, particularly if you're designing for other developers to inherit from your class. As Josh Bloch says in Effective Java, you should design for inheritance or prohibit it. In my view, unless you have a good reason to design for inheritance, you shouldn't do so speculatively.

Does the class implement an interface which you could also implement (possibly by proxying most calls back to an instance of the original)? There's often no really elegant answer here - and the best solution will depend on the exact situation, including what you're trying to add to the class.

If you're not adding any more state - just convenience methods, effectively - then extension methods may work well for you. But they don't change what data an object is capable of storing, so if you need to add your own specialised data, that won't work.

like image 66
Jon Skeet Avatar answered Oct 06 '22 08:10

Jon Skeet


Sounds like a perfect application for extension methods:

MSDN extension method docs

"Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type."

like image 28
Bruce Avatar answered Oct 06 '22 07:10

Bruce


If the class has an internal constructor, and there are no public constructors, then that suggests that the designers did not intend for it to be subclassed. In that case, you can use encapsulation, or you can use extension methods.

like image 3
Eric Smith Avatar answered Oct 06 '22 07:10

Eric Smith