Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

interface implementation and inheritance.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
parez - 26 Mar 2008 18:29 GMT
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.
Peter Duniho - 26 Mar 2008 18:55 GMT
> 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.
>
[quoted text clipped - 3 lines]
>
> 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
parez - 26 Mar 2008 19:22 GMT
On Mar 26, 1:55 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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.
[quoted text clipped - 81 lines]
>
> Pete

Thanks.. I am gonna with solution 1.
parez - 26 Mar 2008 21:32 GMT
On Mar 26, 1:55 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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.
[quoted text clipped - 81 lines]
>
> 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 - 26 Mar 2008 22:06 GMT
> On Mar 26, 1:55 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> wrote:
[quoted text clipped - 97 lines]
>
> 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 Duniho - 26 Mar 2008 23:57 GMT
> 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
parez - 27 Mar 2008 14:37 GMT
On Mar 26, 6:57 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > The following compiles.. Is this expected behavior?
>
[quoted text clipped - 10 lines]
>
> 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
Peter Duniho - 27 Mar 2008 16:21 GMT
> 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 - 27 Mar 2008 17:39 GMT
On Mar 27, 11:21 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > Code does address the original question...
>
[quoted text clipped - 30 lines]
>
> 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 Duniho - 27 Mar 2008 18:04 GMT
> 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
parez - 27 Mar 2008 18:39 GMT
On Mar 27, 1:04 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > Heheh.. May be I mispoke ;)
>
[quoted text clipped - 16 lines]
>
> 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)
Ignacio Machin ( .NET/ C# MVP ) - 26 Mar 2008 18:59 GMT
> 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
Jeff Louie - 27 Mar 2008 06:37 GMT
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.<
Jeff Louie - 27 Mar 2008 11:27 GMT
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
parez - 27 Mar 2008 14:40 GMT
> 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
[quoted text clipped - 33 lines]
>
> *** 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
Jeff Louie - 27 Mar 2008 16:48 GMT
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,
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<<

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.