interface implementation and inheritance.  
Author Message
brettdecarion





PostPosted: Wed Mar 26 12:55:00 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. Is it a good idea(or is it possible) for to implement half an
interface in base class and half in derived class.

e.g Interface IMyinterface has 3 properties.

Class B and C inherit class A, and implement implement IMyinteface.


Class A implements Propety1 of the interface IMyinterface

and class B and Class C implement property 2 and 3.

DotNet203  
 
 
Peter





PostPosted: Wed Mar 26 12:55:00 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Wed, 26 Mar 2008 10:29:01 -0700, parez <EMail@HideDomain.com> wrote:

> Is it a good idea(or is it possible) for to implement half an
> interface in base class and half in derived class.

No...whichever class declares itself as implementing the interface, that=
=

class must fully implement the interface.

Which is not to say that you can't use the class hierarchy in the =

implementation. A base class can use virtual methods, called by the =

interface implementation, which can be overridden by derived classes. A=
=

derived class can use methods from the base class, again called by the =

interface implementation.

So, using your example:

> e.g Interface IMyinterface has 3 properties.
>
> Class B and C inherit class A, and implement implement IMyinteface.
>
> Class A implements Propety1 of the interface IMyinterface
>
> and class B and Class C implement property 2 and 3.

You might have something like this (showing just two properties here for=
=

brevity):

class A
{
protected int Property1Impl
{
get { ... }
set { ... }
}
}

class B : A, IMyInterface
{
public int Property1
{
get { return Property1Impl; }
set { Property1Impl =3D value; }
}

public int Property2
{
get { ... }
set { ... }
}
}

In that example, only B actually implements the interface, but it inheri=
ts =

an implementation of the interface, used explicitly internally, from A.

Or even something like this:

abstract class A : IMyInterface
{
public int Property1
{
get { ... }
set { ... }
}

public abstract int Property2 { get; set; }
}

class B : A
{
public int Property2
{
get { ... }
set { ... }
}
}

In this example, it's A that implements the interface, but it leaves one=
=

property unimplemented, requiring any derived classes to implement it. =
Of =

course, this means that you can't instantiate A, because it's abstract. =
=

If you want to be able to instantiate A _and_ have it implement =

IMyInterface, obviously it has to have an actual implementation in class=
A.

In that case, a third alternative would be to make the abstract property=
a =

virtual property instead. Then A has a default implementation that othe=
r =

classes can override.

Pete
 
 
Ignacio





PostPosted: Wed Mar 26 12:59:11 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 26, 1:29=A0pm, parez <EMail@HideDomain.com> wrote:
> Is it a good idea(or is it possible) for =A0to implement half an
> interface in base class and half in derived class.

It's possible IF and only IF the base class is abstract. and simply
declare the method of the interface it's implementing (but do not
provide an implementation).

The the devided class must implement the abstract members.


There is no way for the base class to not declare ALL the members of
the inteface
 
 
parez





PostPosted: Wed Mar 26 13:22:14 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 26, 1:55 pm, "Peter Duniho" <EMail@HideDomain.com>
wrote:
> On Wed, 26 Mar 2008 10:29:01 -0700, parez <EMail@HideDomain.com> wrote:
> > Is it a good idea(or is it possible) for to implement half an
> > interface in base class and half in derived class.
>
> No...whichever class declares itself as implementing the interface, that
> class must fully implement the interface.
>
> Which is not to say that you can't use the class hierarchy in the
> implementation. A base class can use virtual methods, called by the
> interface implementation, which can be overridden by derived classes. A
> derived class can use methods from the base class, again called by the
> interface implementation.
>
> So, using your example:
>
> > e.g Interface IMyinterface has 3 properties.
>
> > Class B and C inherit class A, and implement implement IMyinteface.
>
> > Class A implements Propety1 of the interface IMyinterface
>
> > and class B and Class C implement property 2 and 3.
>
> You might have something like this (showing just two properties here for
> brevity):
>
> class A
> {
> protected int Property1Impl
> {
> get { ... }
> set { ... }
> }
> }
>
> class B : A, IMyInterface
> {
> public int Property1
> {
> get { return Property1Impl; }
> set { Property1Impl = value; }
> }
>
> public int Property2
> {
> get { ... }
> set { ... }
> }
> }
>
> In that example, only B actually implements the interface, but it inherits
> an implementation of the interface, used explicitly internally, from A.
>
> Or even something like this:
>
> abstract class A : IMyInterface
> {
> public int Property1
> {
> get { ... }
> set { ... }
> }
>
> public abstract int Property2 { get; set; }
> }
>
> class B : A
> {
> public int Property2
> {
> get { ... }
> set { ... }
> }
> }
>
> In this example, it's A that implements the interface, but it leaves one
> property unimplemented, requiring any derived classes to implement it. Of
> course, this means that you can't instantiate A, because it's abstract.
> If you want to be able to instantiate A _and_ have it implement
> IMyInterface, obviously it has to have an actual implementation in class A.
>
> In that case, a third alternative would be to make the abstract property a
> virtual property instead. Then A has a default implementation that other
> classes can override.
>
> Pete

Thanks.. I am gonna with solution 1.
 
 
parez





PostPosted: Wed Mar 26 15:32:01 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 26, 1:55 pm, "Peter Duniho" <EMail@HideDomain.com>
wrote:
> On Wed, 26 Mar 2008 10:29:01 -0700, parez <EMail@HideDomain.com> wrote:
> > Is it a good idea(or is it possible) for to implement half an
> > interface in base class and half in derived class.
>
> No...whichever class declares itself as implementing the interface, that
> class must fully implement the interface.
>
> Which is not to say that you can't use the class hierarchy in the
> implementation. A base class can use virtual methods, called by the
> interface implementation, which can be overridden by derived classes. A
> derived class can use methods from the base class, again called by the
> interface implementation.
>
> So, using your example:
>
> > e.g Interface IMyinterface has 3 properties.
>
> > Class B and C inherit class A, and implement implement IMyinteface.
>
> > Class A implements Propety1 of the interface IMyinterface
>
> > and class B and Class C implement property 2 and 3.
>
> You might have something like this (showing just two properties here for
> brevity):
>
> class A
> {
> protected int Property1Impl
> {
> get { ... }
> set { ... }
> }
> }
>
> class B : A, IMyInterface
> {
> public int Property1
> {
> get { return Property1Impl; }
> set { Property1Impl = value; }
> }
>
> public int Property2
> {
> get { ... }
> set { ... }
> }
> }
>
> In that example, only B actually implements the interface, but it inherits
> an implementation of the interface, used explicitly internally, from A.
>
> Or even something like this:
>
> abstract class A : IMyInterface
> {
> public int Property1
> {
> get { ... }
> set { ... }
> }
>
> public abstract int Property2 { get; set; }
> }
>
> class B : A
> {
> public int Property2
> {
> get { ... }
> set { ... }
> }
> }
>
> In this example, it's A that implements the interface, but it leaves one
> property unimplemented, requiring any derived classes to implement it. Of
> course, this means that you can't instantiate A, because it's abstract.
> If you want to be able to instantiate A _and_ have it implement
> IMyInterface, obviously it has to have an actual implementation in class A.
>
> In that case, a third alternative would be to make the abstract property a
> virtual property instead. Then A has a default implementation that other
> classes can override.
>
> Pete

I think the separtion of properties works..


I have IMyInterface:interface2

interface2 has property1 defined in it.

and IMyInterface has property2 and property3



Class B implements IMyInterface. and inherits class A
Class A implements interface2.

And this works..



 
 
parez





PostPosted: Wed Mar 26 16:06:02 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 26, 4:32 pm, parez <EMail@HideDomain.com> wrote:
> On Mar 26, 1:55 pm, "Peter Duniho" <EMail@HideDomain.com>
> wrote:
>
>
>
> > On Wed, 26 Mar 2008 10:29:01 -0700, parez <EMail@HideDomain.com> wrote:
> > > Is it a good idea(or is it possible) for to implement half an
> > > interface in base class and half in derived class.
>
> > No...whichever class declares itself as implementing the interface, that
> > class must fully implement the interface.
>
> > Which is not to say that you can't use the class hierarchy in the
> > implementation. A base class can use virtual methods, called by the
> > interface implementation, which can be overridden by derived classes. A
> > derived class can use methods from the base class, again called by the
> > interface implementation.
>
> > So, using your example:
>
> > > e.g Interface IMyinterface has 3 properties.
>
> > > Class B and C inherit class A, and implement implement IMyinteface.
>
> > > Class A implements Propety1 of the interface IMyinterface
>
> > > and class B and Class C implement property 2 and 3.
>
> > You might have something like this (showing just two properties here for
> > brevity):
>
> > class A
> > {
> > protected int Property1Impl
> > {
> > get { ... }
> > set { ... }
> > }
> > }
>
> > class B : A, IMyInterface
> > {
> > public int Property1
> > {
> > get { return Property1Impl; }
> > set { Property1Impl = value; }
> > }
>
> > public int Property2
> > {
> > get { ... }
> > set { ... }
> > }
> > }
>
> > In that example, only B actually implements the interface, but it inherits
> > an implementation of the interface, used explicitly internally, from A.
>
> > Or even something like this:
>
> > abstract class A : IMyInterface
> > {
> > public int Property1
> > {
> > get { ... }
> > set { ... }
> > }
>
> > public abstract int Property2 { get; set; }
> > }
>
> > class B : A
> > {
> > public int Property2
> > {
> > get { ... }
> > set { ... }
> > }
> > }
>
> > In this example, it's A that implements the interface, but it leaves one
> > property unimplemented, requiring any derived classes to implement it. Of
> > course, this means that you can't instantiate A, because it's abstract.
> > If you want to be able to instantiate A _and_ have it implement
> > IMyInterface, obviously it has to have an actual implementation in class A.
>
> > In that case, a third alternative would be to make the abstract property a
> > virtual property instead. Then A has a default implementation that other
> > classes can override.
>
> > Pete
>
> I think the separtion of properties works..
>
> I have IMyInterface:interface2
>
> interface2 has property1 defined in it.
>
> and IMyInterface has property2 and property3
>
> Class B implements IMyInterface. and inherits class A
> Class A implements interface2.
>
> And this works..


The following compiles.. Is this expected behavior?
class A
{
public string prop2
{
get;
set;
}

}
class B : A, Interface1
{

public string prop1
{
get;
set;
}

}

public interface Interface2
{
string prop2 { get; set; }
}


public interface Interface1 : Interface2
{
string prop1 { get; set; }
}


 
 
Peter





PostPosted: Wed Mar 26 17:57:06 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Wed, 26 Mar 2008 14:06:02 -0700, parez <EMail@HideDomain.com> wrote:

> The following compiles.. Is this expected behavior?

Is what "expected behavior?

If you're using C# 3.0, then yes...I don't see any reason what you posted
wouldn't compile. But whether it does what you really want it to do, I
can't say. You haven't declared class A as implementing Interface2, even
though it does appear to. Nor does the code appear to address the
question you originally asked (that is, having the implementation of a
single interface spread across multiple classes).

But if the code does what you want it to do, seems fine to me. :)

Pete
 
 
Jeff





PostPosted: Thu Mar 27 00:37:36 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. Parez... As answered in this thread, it is possible to implement part of
an interface in a base class and complete the implementation in the
derived class. Given an interface ISort:

interface ISort {
void Sort(Array array);
int CompareTo(Object obj);
}

You could write an abstract class that implements a complex Sort
algorithm as in:

abstract class MySortBase : ISort
{
public void Sort(Array array) {
System.Console.WriteLine("Sorted");
} // dummy implementation
public abstract int CompareTo(Object obj);
}

Other coders could then use your complex sort algorithm by inheriting
from MySortBase and implementing the CompareTo method as in:

class MySort : MySortBase
{
public override int CompareTo(object obj)
{
....
}
}


Regards,
Jeff
>Is it a good idea(or is it possible) for to implement half an interface
in base class and half in derived class.<

*** Sent via Developersdex http://www.developersdex.com ***
 
 
parez





PostPosted: Thu Mar 27 08:37:21 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 26, 6:57 pm, "Peter Duniho" <EMail@HideDomain.com>
wrote:
> On Wed, 26 Mar 2008 14:06:02 -0700, parez <EMail@HideDomain.com> wrote:
> > The following compiles.. Is this expected behavior?
>
> Is what "expected behavior?
>
> If you're using C# 3.0, then yes...I don't see any reason what you posted
> wouldn't compile. But whether it does what you really want it to do, I
> can't say. You haven't declared class A as implementing Interface2, even
> though it does appear to. Nor does the code appear to address the
> question you originally asked (that is, having the implementation of a
> single interface spread across multiple classes).
>
> But if the code does what you want it to do, seems fine to me. :)
>
> Pete

Code does address the original question...

Is it a good idea(or is it possible) for to implement half an
interface in base class and half in derived class.

Half (i should have said some) of MyInterface is implemented in
Class A (base class) and half in Class B.

Yes.. It does do what I want it to do..

And why should it work only in c# 3.0? I am using .net 3.0( i think is
c# 2.0)


TIA




 
 
parez





PostPosted: Thu Mar 27 08:40:35 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 27, 1:37 am, Jeff Louie <EMail@HideDomain.com> wrote:
> Parez... As answered in this thread, it is possible to implement part of
> an interface in a base class and complete the implementation in the
> derived class. Given an interface ISort:
>
> interface ISort {
> void Sort(Array array);
> int CompareTo(Object obj);
> }
>
> You could write an abstract class that implements a complex Sort
> algorithm as in:
>
> abstract class MySortBase : ISort
> {
> public void Sort(Array array) {
> System.Console.WriteLine("Sorted");
> } // dummy implementation
> public abstract int CompareTo(Object obj);
> }
>
> Other coders could then use your complex sort algorithm by inheriting
> from MySortBase and implementing the CompareTo method as in:
>
> class MySort : MySortBase
> {
> public override int CompareTo(object obj)
> {
> ....
> }
> }
>
> Regards,
> Jeff>Is it a good idea(or is it possible) for to implement half an interface
>
> in base class and half in derived class.<
>
> *** Sent via Developersdexhttp://www.developersdex.com***
Hi Jeff,

In my example the derived class implemented the interface. I think i
should have made that clear.

Class A -- base class
class B -- derived class

class B: Myinterface



 
 
Peter





PostPosted: Thu Mar 27 10:21:33 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Thu, 27 Mar 2008 06:37:21 -0700, parez <EMail@HideDomain.com> wrote:

> Code does address the original question...
>
> Is it a good idea(or is it possible) for to implement half an
> interface in base class and half in derived class.

I don't see how.

> Half (i should have said some) of MyInterface is implemented in
> Class A (base class) and half in Class B.

Class A implements a method that looks like Interface2, but the class
itself does not implement that interface, nor is that interface also being
implemented (in any way) by Interface1.

If you believe it answers/addresses your question, I guess that's fine.
But I'm not seeing it. I believe that if you feel it does answer your
question, then the question you've stated here is not actually your
question.

> Yes.. It does do what I want it to do..
>
> And why should it work only in c# 3.0? I am using .net 3.0( i think is
> c# 2.0)

The code you posted uses the "automatic property" syntax, which isn't
available in C# 2.0. So if it compiles without error, you must be using
C# 3.0. You can still target earlier versions of .NET with C# 3.0, if
you're using VS 2008, so the .NET version doesn't necessarily tell you
which version of C# you're using.

If you're using VS 2005, then I don't see how the code you posted could
compile.

Pete
 
 
parez





PostPosted: Thu Mar 27 11:39:13 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 27, 11:21 am, "Peter Duniho" <EMail@HideDomain.com>
wrote:
> On Thu, 27 Mar 2008 06:37:21 -0700, parez <EMail@HideDomain.com> wrote:
> > Code does address the original question...
>
> > Is it a good idea(or is it possible) for to implement half an
> > interface in base class and half in derived class.
>
> I don't see how.
>
> > Half (i should have said some) of MyInterface is implemented in
> > Class A (base class) and half in Class B.
>
> Class A implements a method that looks like Interface2, but the class
> itself does not implement that interface, nor is that interface also being
> implemented (in any way) by Interface1.
>
> If you believe it answers/addresses your question, I guess that's fine.
> But I'm not seeing it. I believe that if you feel it does answer your
> question, then the question you've stated here is not actually your
> question.
>
> > Yes.. It does do what I want it to do..
>
> > And why should it work only in c# 3.0? I am using .net 3.0( i think is
> > c# 2.0)
>
> The code you posted uses the "automatic property" syntax, which isn't
> available in C# 2.0. So if it compiles without error, you must be using
> C# 3.0. You can still target earlier versions of .NET with C# 3.0, if
> you're using VS 2008, so the .NET version doesn't necessarily tell you
> which version of C# you're using.
>
> If you're using VS 2005, then I don't see how the code you posted could
> compile.
>
> Pete

Heheh.. May be I mispoke ;)

This is what i want to do with my above listed code
B b = new B();
Interface1 i1 = b;
MessageBox.Show(i1.prop2);


Also I am using VS2008 and target framework is 3.0


 
 
Peter





PostPosted: Thu Mar 27 12:04:07 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Thu, 27 Mar 2008 09:39:13 -0700, parez <EMail@HideDomain.com> wrote:

> Heheh.. May be I mispoke ;)
>
> This is what i want to do with my above listed code
> B b =3D new B();
> Interface1 i1 =3D b;
> MessageBox.Show(i1.prop2);

Seems fine to me. B implements Interface1, which in turn implements =

Interface2, which in turn includes "prop2", which is implemented by A, =

which is inherited by B.

I can't say it's the most obvious, most simple design I've ever run into=
, =

but it should work.

> Also I am using VS2008 and target framework is 3.0

Then I don't see any reason the code you posted wouldn't work. VS 2008 =
=

supports C# 3.0.

Pete
 
 
parez





PostPosted: Thu Mar 27 12:39:24 CDT 2008 Top

Visual C#.Net >> interface implementation and inheritance. On Mar 27, 1:04 pm, "Peter Duniho" <EMail@HideDomain.com>
wrote:
> On Thu, 27 Mar 2008 09:39:13 -0700, parez <EMail@HideDomain.com> wrote:
> > Heheh.. May be I mispoke ;)
>
> > This is what i want to do with my above listed code
> > B b = new B();
> > Interface1 i1 = b;
> > MessageBox.Show(i1.prop2);
>
> Seems fine to me. B implements Interface1, which in turn implements
> Interface2, which in turn includes "prop2", which is implemented by A,
> which is inherited by B.
>
> I can't say it's the most obvious, most simple design I've ever run into,
> but it should work.
>
> > Also I am using VS2008 and target framework is 3.0
>
> Then I don't see any reason the code you posted wouldn't work. VS 2008
> supports C# 3.0.
>
> Pete

In my case..

A= BaseForm
Interface2 is Error Related stuff.( user error stuff.. things like
missing fields etc.)
All errors are handled the same way.

B=SomeForm
Interface1 represents the fields on that form.

I pass interface1 to a class which populates the form.

SomeClass.PopulateSomeForm(Interface1 form)