interface implementation and inheritance.

P

parez

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

Ignacio Machin ( .NET/ C# MVP )

Is it a good idea(or is it possible) for  to 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
 
P

parez

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:





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

parez

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:





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

parez

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; }
}
 
J

Jeff Louie

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

Jeff Louie

This may be a better example. I should not try to code past my bedtime.

interface ISort {
void Sort(object[] array);
int Compare(Object obj1, Object obj2);
}

Regards,
Jeff
 
P

parez

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
 
P

parez

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
 
J

Jeff Louie

Parez.. Not a problem:

interface ISort {
void Sort(Object[] array);
int Compare(Object obj1, Object obj2);
}

abstract class MySortBase
{
public void Sort(Object[] array) {
... calls Compare
}
public abstract int Compare(Object obj1, Object obj2);
}

class MySortString : MySortBase, ISort
{
// ASSERT each parameter is null or is a string
public override int Compare(Object obj1, Object obj2)
{
...
}
}

USAGE:

static void Main(string[] args)
{
MySortString mss = new MySortString();
String[] myValues = {"puff", "huff", null};
mss.Sort(myValues);
}

Regards,
Jeffshould have made that clear.

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

class B: Myinterface<<
 
P

parez

I don't see how.


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.



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
 
P

parez

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.


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)
 

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

Top