Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface inheritance: is extending properties possible?

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.

like image 839
Malki Avatar asked Aug 18 '10 20:08

Malki


People also ask

Is inheritance possible in interface?

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.

CAN interface can have properties?

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.

Can one interface extend another interface in C#?

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.

What is interface inheritance in Java how it can be inherited?

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).


2 Answers

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; } } 
  1. If an object implements IBase, you can read its Property1 property.
  2. If an object implements 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.

like image 193
Dan Tao Avatar answered Sep 25 '22 19:09

Dan Tao


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

like image 38
Chris Marisic Avatar answered Sep 23 '22 19:09

Chris Marisic