1

Why can't I access the base class (testClass) properties through it's interface (ItestClass)? I have created the interface to avoid the actual Control (winforms/wpf) properties to be visible in the third class (newClass). if that is not possible is there a better way?

public class testClass : Control, ItestClass
{
    public int Property1 { set; get; }
    public int Property2 { set; get; }
    public testClass() { }
}
public interface ItestClass
{
    int Property1 { get; set; }
    int Property2 { get; set; }
}

public class newClass : ItestClass
{
    public newClass()
    {
        // Why are the following statements are not possible?
        Property1 = 1;
        // OR
        this.Property1 = 1;
    }
}

1 Answer 1

5

The interface doesn't actually implement the properties -- you will still need to define them in the implementing class:

public class newClass : ItestClass
{
    int Property1 { get; set; }
    int Property2 { get; set; }

    // ...
}

Edit

C# doesn't support multiple inheritance, so you can't have testClass inherit both Control and another concrete class. You could always use composition instead, though. For example:

public interface ItestClassProps
{
    public ItestClass TestClassProps { get; set; }
}

public class testClass : Control, ItestClassProps
{
    public ItestClass TestClassProps { set; get; }

    public testClass() { }
}
Sign up to request clarification or add additional context in comments.

10 Comments

@mason not in newClass.
the thing is I simply didn't want to directly inherit testClass, as it would also show all the Control properties, instead I inherited it's interface to avoid that.
so basically I need to redefine them in third class (newClass)?
@Johnny yes, since C# doesn't support multiple inheritance, you have to use composition in this case.
@Johnny so basically, either use encapsulation by passing a reference to "ItestClass" instead of "testClass", or use composition by giving "testClass" an "ItestClass" property (see my edit above).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.