I want to do this:
interface IBase { string Property1 { get; } } interface IInherited : IBase { string Property1 { get; set; } }
So that IInherited
would have the inherited property Property1
with added functionality to allow set
.
Is that possible? What's the syntax?
EDIT: please notice I put the word "inherited" in bold face. I am asking specifically about inheriting the property, not hiding it behind a new one.
An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.
Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface's members will be given by class who implements the interface implicitly or explicitly.
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.
It is the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class. Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
If the fact that the only way to do this is by using the new
keyword bothers you, then in my opinion you're thinking about interfaces wrong.
Sure, you could say that IInherited
"inherits from" IBase
; but what does that really mean? These are interfaces; they establish code contracts. By hiding the IBase.Property1
property with new string Property1 { get; set; }
, you are not shadowing any functionality. Thus the traditional reason that a lot of developers consider hiding to be a "bad" thing -- that it violates polymorphism -- is irrelevant in this case.
Ask yourself: what really matters when it comes to interfaces? They provide a guarantee of responding to certain method calls, right?
So, given the following two interfaces:
interface IBase { string Property1 { get; } } interface IInherited : IBase { new string Property1 { set; } }
IBase
, you can read its Property1
property.IInherited
, you can read its Property1
property (just as with an IBase
implementation), and you can also write to it.Again, there's really nothing problematic here.
Hiding a member is violating the Liskov Substitution Principle and pretty much just shouldn't be done, ever. By hiding this member you are introducing a very difficult to locate bug since 2 different outcomes will occur depending whether you cast the object as ((IInherited).Property1)
or cast it to ((IBase).Property1)
.
http://en.wikipedia.org/wiki/Liskov_substitution_principle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With