About Interface


T

Tony

Hello!

The keword abstract and sealed are not allowed in interfaces.
I can understand that abstract is not allowed because an interface is
completely abstract
and contains no implementation.

As mentioned before sealed is not allowed in an interface because it makes
not sense is the reason that is mentioned but if we have
this construction it would make sense. Here we are not allowed to inherit
the interface IMyBaseInterface

public sealed interface IMyInterface : IMyBaseInterface
{
....
}

//Tony
 
Ad

Advertisements

M

Morten Wennevik [C# MVP]

Hi Tony,

Why shouldn't you be able to inherit from an interface. You can't change
the original implementation. Consider the example

interface Unherited
{
string Text { get;}
}

interface Inherited : Unherited
{
new string Text { set;}
}

class MyClass : Inherited
{
#region Inherited Members

public string Text
{
set { }
}

#endregion

#region Unherited Members

string Unherited.Text
{
get { return "Hello"; }
}

#endregion
}

You still need to implement a getter property so

string s = ((Unherited)MyClass).Text;

is still valid.
 
G

Göran Andersson

Tony said:
Hello!

The keword abstract and sealed are not allowed in interfaces.
I can understand that abstract is not allowed because an interface is
completely abstract
and contains no implementation.

As mentioned before sealed is not allowed in an interface because it makes
not sense is the reason that is mentioned but if we have
this construction it would make sense. Here we are not allowed to inherit
the interface IMyBaseInterface

public sealed interface IMyInterface : IMyBaseInterface
{
...
}

//Tony
If you could make an interface sealed then it would be totally useless,
as you can neither inherit it nor implement it.
 
R

Rene

So let's just say for now that you could seal the interface, what would be
the difference between:

----------------------------
// The way thing work now:

interface IA
{
void WhateverOnA();
}

interface IB : IA
{
void WhateverOnB();
}

class SomeClass : IB
{
public void WhateverOnA()
{
throw new NotImplementedException();
}

public void WhateverOnB()
{
throw new NotImplementedException();
}
}

------------ And --------------
// Allowing interfaces to be sealed:

sealed interface IA // Sealed
{
void WhateverOnA();
}

interface IB // Cant inherit from IA
{
void WhateverOnB();
}

class SomeClass : IA, IB
{
public void WhateverOnA()
{
throw new NotImplementedException();
}

public void WhateverOnB()
{
throw new NotImplementedException();
}
}

----------------------------

What exactly was accomplished here? What was prevented from happening?

The first example show interfaces being inherited the way they work now, the
second example shows interface "IA" sealed so there was no way to inherit
form it but never the less, at the end, I was able to accomplish the exact
same thing as on the first example.

Cheers.
 
G

Göran Andersson

Peter said:
To be fair to Tony, that all depends on the definition of "sealed".
There's no reason that the "sealed" keyword _must_ mean that an
interface can't be implemented. The language _could_ treat interface
inheritance differently from interface implementation for the purpose of
an interface being "sealed".
It could, but that wouldn't really make sense. To be reasonably
consistent, a sealed interface would only be allowed to be implemented
by a sealed class, otherwise implementing the interface would
effectively un-seal it.
 
Ad

Advertisements

G

Göran Andersson

Peter said:
The whole point of this thread is that "sealed" doesn't make sense
regardless. The phrase "wouldn't really make sense" is useless for
distinguishing between two different interpretations, neither of which
make sense.
So what are you arguing about, then? Do you want it to not make sense?
You are not making very much sense...
My point is that your intepretation isn't the only one
I never said that it was. Stop putting words in my mouth. It's very
annoying when you do that.
I agree that's one
possible interpretation, but it's not the only one, and even if it were,
it's not the explanation for why using "sealed" on interfaces doesn't
make sense.
So you are the one that is deciding that? Noone else has the right to
have an opinion?
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top