Is it possible to hide properties from a derived class?

Class A derives from class B. In class A, I want to hide some properties inherited from class B. Therefore, when an instance is created from class A, I do not want to set some properties from class B.

Is it possible?

+3
source share
6 answers

No, it will defeat the goal of inheritance. Your class A is B, so it has propertiesB

+8
source

Can I use the private access specifier for your property that you want to hide?

if not, then please follow this link - can help you - C # hide some properties of the base class from dervied classes

+2
source

A B, , A B, ; B.

: a Dog Animal. , Animal , a Dog ; .

+2
source

To do this, instead of working on closing the property, avoid inheritance and apply composition (for example, use interfaces).

+1
source

Do you mean:

class A {
    public virtual int X {
        get { return 1; }
    }
}

class B : A {
    public sealed override int X {
        get { return 2; }
    }
}

class C : B {
    public override int X {
        get { return -1; }
    }
}

If yes, yes. (The above gives a compile-time error in C). The point here is that he Ahad the property, Bimplemented it, and wanted to prevent one of the subclasses from doing this.

0
source

Source: https://habr.com/ru/post/1762414/


All Articles