Anthony
Anthony

Reputation: 437

cannot access protected constructor in abstract class

I'm creating a unit test code for an abstract class. here is a snippet from that class:

public abstract class Component
    {

        private eVtCompId mComponentId;
        private eLayer mLayerId;
        private IF_SystemMessageHandler mLogger;

        protected Component(eVtCompId inComponentId, eLayer inLayerId, IF_SystemMessageHandler inMessageHandler)
        {
            mLayerId = inLayerId;
            mComponentId = inComponentId;
            mLogger = inMessageHandler;
        }

What I have in the constructor's parameters are two enums followed by an interface.

Here is a snippet from my unit test code:

Component_Accessor target = new Component_Accessor(eVtCompId.MasterSWCommDevice, eLayer.Foundation, new MySysMsgHandler());

I keep getting the error message "Component_Accessor does not contain a constructor that takes '3' arguments". I can't seem to understand why that is happening. When I removed the abstract keyword, the unit test works fine.

I don't get why the unit test can't seem to "see" the constructor if the class is set to abstract. Can anyone explain why this is happening? Thanks in advance.

Upvotes: 0

Views: 3238

Answers (1)

horgh
horgh

Reputation: 18534

You cannot create an instance of an abstract class. protected constructors are visible only for derived classes. Read Accessibility Levels (C# Reference) to clear up differences among access modifiers and their influence in various (including class) scopes.

From MSDN:

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

Besides, Component and Component_Accessor are definitely different types.

Upvotes: 2

Related Questions