tags:

views:

486

answers:

5

Hello!

Lets say I inherit a class, that has several public properties and/or methods, however I do not want them to be public properties/methods of my class - in other words, I want to make those properties protected properties of my class.

Can this be achieved?

I hope I was clear enough, if not please let me know, will try to explain myself better.

EDIT:

Right, thank you all for answers however I don't think I was clear enough. What I am trying to accomplish is this:

I wrote a windows control that extends ListView control. ListView has a public collection Items, that can be modified. That's all fine, however I wrote new methods for adding items to listview because of the extra data I need.

It all works great so far, however the Items collection can still be modified by anything, which is a problem, because if an item is added by direct manipulation of Items collection not all data I need is gathered, thus causing an error.

Since we hope to reuse this control several times in different projects, we are afraid that sooner or later, the default way of adding items to Items collection will be used (just a matter of time really). We are just looking for a way to prevent that from happening, like throwing an exception when Items collection gets bigger, but the way it was intended.

I hope this all makes sense now.

+1  A: 

You can't do this by inheritance. This is actually what inheritance is about. An is-a relation cannot reduce the interface. The derived class must be a full representation of the base class.

Turn your inheritance into a reference. You reuse the implementation of your "base class" by calling it. (Composition instead of inheritance.) If you need polymorphism, add a common interface or move the common part into a separate abstract base class.

Stefan Steinegger
+3  A: 

No, you cannot do that. The best you can do is creating a class and wrap the base class insted of deriving from it - but this will of course break inheritance. (I assume you cannot modify the base class. If you can, you should rethink the design because it looks like your new class should not derive from the base class.)

class BaseClass
{
     public String IWantThis { get; set; }
     public String IDoNotWantThis { get; set; }
}

class MyClass
{
    private BaseClass baseClass = new BaseClass();

    public String IWantThis
    {
        get { return this.baseClass.IWantThis; }
        set { this.baseClass.IWantThis = value; }
    }
}
Daniel Brückner
+4  A: 

Inheritance is all about saying "You can use this derived class in the same way as you can use the base class, and it provides specialized behaviour."

If you can't use the derived class in the same way as the base class, then you shouldn't be using inheritance: you're breaking Liskov's Substitutability Principle. In such a case, use composition instead of inheritance. (Personally I don't use class-to-class inheritance that much anyway, far preferring composition - I find there are relatively few cases where the specialization aspect really works without issues. When it does work it's great though!)

Jon Skeet
The problem with WinForms controls and composition (UserControl containing a ListView in this case) is that you have to reimplement so much stuff in your UserControl to let it behave like a ListView.So it is very imprectical to use composition here.
rstevens
A: 

I not use AOP, perhaps PostSharp, to put around the Property you don't want used, and then you can handle it in some appropriate fashion with your aspect.

James Black
+4  A: 

Never say never. This is probably not the best idea but it seems to work for me. This hides items by re-implementing it in the subclass and then hiding it using attributes. I added in a "CustomExposedItems" property so you can see that the existing items are still accessible in the underlying ListView.

public partial class CustomListView : ListView
{
    public CustomListView()
    {
        InitializeComponent();
    }

    public System.Windows.Forms.ListView.ListViewItemCollection CustomExposedItems
    {
        get
        {
            return base.Items;
        }

    }

    [EditorBrowsable(EditorBrowsableState.Never)]    
    [Browsable(false)]    
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [Obsolete("Use the new custom way of adding items xyz")]
    public new System.Windows.Forms.ListView.ListViewItemCollection Items    
    { 
        get { throw new NotSupportedException(); }    
    }

}
Robert Venables
+1; It's still possible to access the original Items via casting. But I think it is the best you can get without using a UserControl containing the ListView (which leads to even worse problems to solve).
rstevens