Link to home
Start Free TrialLog in
Avatar of paulwhelan
paulwhelan

asked on

Interface - you cant implement one - what can you do?

Hi
Just struggling around the term interface.
I know you can;t implement an interface so what can you do?
And why use them?

Also whats the difference between inheritance and an interface?

Thanks
Paul
Avatar of 2266180
2266180
Flag of United States of America image

hm .. I think you should really read a little on OOP. interfaces MUST be implemented. inheritance is a petter (teorethical stuff) and interface is part of the practical stuff (not related to inheritance, though interfaces can be extended)

some articles:
http://zone.ni.com/devzone/conceptd.nsf/webmain/DCAF6FDB7A3BC02486256D4F00721675
http://java.sun.com/docs/books/tutorial/java/concepts/index.html
http://www.zib.de/visual/people/mueller/Course/Tutorial/tutorial.html
http://zone.ni.com/devzone/conceptd.nsf/webmain/0E0CE2D6F70F502386256CDA00753BAD
http://www.brpreiss.com/books/opus6/html/page588.html

the language does not count in order to understand the basics ;)
Avatar of paulwhelan
paulwhelan

ASKER

Sorry ciuly just looking for a simple explanation. I get lost in the mumbo jumbo =)

So you must implement an interface.
Don't you have to implement a class too?

Not seeingmuch of a difference there.

Whats the point of interfaces?

thanks
hm... you got it wrong. probably it's the words.

when you say to implement an interface, you mean to assign an implementation (class) to that interface.
when you say to implement a class, you say to write code for that class., but this expression is very rarely used. it's is more correct to say that you implement (write) code for a certain class.

the idea behind the interface is that you don't have to write any kind of implementation and you can still use it around the code, thus giving you an abstract layer: and later you can make as many implementations as you like. reading about all this is mandatory. I'm not such  a good explainer anyway:)
ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, It sounds like Paul prefers real life already done example.  Here is one

I have an application that revolves round Benchmarking (Winform app).  The best UI design I could come up was wizard style, so I have a Wizard form which has a side panel for menus/ decorations, a bottom panel for status and a large maion panel that will contain usercontrols (I have 9) these user controls represent wizard steps.

Now when the wizard form loads, I load an array of user controls this

      // Initialise our array of controls. Each control is a step in the wizard.
      this.wizardSteps = new UserControl[9];
      Controls.Processing.Start start = new Controls.Processing.Start();
      this.wizardSteps[0] = start;
      Controls.Processing.Introduction intro = new Controls.Processing.Introduction();
      this.wizardSteps[1] = intro;
                ....
                and so on for all 9 user controls

What i found out was that i am going to need to question the usercontrols if they are dirty (something changed, control text changed etc) and if so I want it to update the Benchmark (which means it needs reference to the Benchmark object which is in the wizard form)

I also wanted usercontrols to populate their controls as and when i wanted.

Now from the wizard form (class) there is not way to tell user control to populate its controls (say from xml file) then it hit me, that is what Interfaces are for (Inheritance won't do here because these controls are very different, no IS A relationship here, HOWEVER, they all CAN perform certain things (PopulateControls) or have certain properties (IsDirty) although they are very different otherwise), so i ended up creating the following interface

        public interface IProcessingWizardControl
        {
            #region Public Properties
            bool IsDirty { get; set; }
            Objects.ProcessingBenchmark CurrentBenchmark { get; set; }
            #endregion

            #region Public Methods
            void PopulateControls();
            #endregion
        }

Now in my Wizard form, I get the current usercontrol as follows (a property in my wizard form)

      public IProcessingWizardControl CurrentStepControl
      {
            get
            {
                  // Cast the current step control to our interface.
                  return (IProcessingWizardControl)this.wizardSteps[this.currentStep];
            }
      }

Remember wizardSteps is the array of user controls. currentStep is a local variable that I increment or decrement when user clicks next and back buttons

Now in the back button (for example) I do the following (Notice how I can access the IsDirty property via the Interface implementation)

      if (this.CurrentStepControl.IsDirty)
            {
                  // Make sure we save any changes.
                  this.isDirty = true;
            }

      this.currentStep++;
      this.DisplayCurrentStep();

CurrentStepControl is the property we defined above and will return the current user control displayed

And in the DisplayCurrentStep above I have code like this

      // Clear the main panel.
      this.pnlWizard.Controls.Clear();
      // Set the current benchmark for the control.
      this.CurrentStepControl.CurrentBenchmark = this.currentBenchmark;
      // Display the current step control.
      this.pnlWizard.Controls.Add(this.wizardSteps[this.currentStep]);
      // Populate controls if required.
      this.CurrentStepControl.PopulateControls();
      this.ResizeCurrentControl();

Again in the ResizeCurrentControl I can cast back from the Interface to control so that I can access Height, Width properties etc as follows

      private void ResizeCurrentControl()
      {
            Control currentControl = (Control)this.CurrentStepControl;
            if (currentControl != null)
            {
                  currentControl.Height = this.pnlWizard.Height - 8;
                  currentControl.Width = this.pnlWizard.Width - 8;
            }
      }


I hope that is good enough example of how interfaces can help

Cheers
Hassan










mrichmon,
Could I not do you Email, Paper, StoneTablet example with classes and not an interface?
Thanks
Paul
Also whats the deifference between an interface and an abstract class?

Thanks
Paul
when I said that you should read a little on OOP, I ment it. these are the very basic of OOP. we cannot explain you everything from OOP just becaus you don't want to read on you own.
Seriously now: if you want to understand about OOP, either buy a good book on this, or read some tutorials/articles freely available on the net (I gave you a few).

just out of curiousity: why don't you follow my advice and read a little on this?
?

Ok fair enough ...

I've read a book on OOP and C#.net.
Concepts such as Interfaces and their difference to abstract classes aren't exactly easy to understand for everyone.

If you can't help then please don't post.

Paul
I must agree with ciuly.  You will be better off doing some reading which will cover the topics. People here cannot cover the topic fully in an answer to your question or at least it would be quite time consuming for them to do so
>>Could I not do you Email, Paper, StoneTablet example with classes and not an interface?

You could.  And depending on the situation you may want to.  BUt you could write interfaces that are more generic as well.  

For example, consider the pre-defined interface IComparer.  If your class implements this interface - which can be applied across many many types of objects, it means your object can use the comparison operators.  You wouldn't want to use inheritance for this since it is only one small function and your object may be in a larger scheme already inheriting from other objetcs.  But you want all of those to be able to be compared, you could implement the interface.  Another such one is ISortable, which allows your objects to use a similar sorting method as built in objects as long as you implement this interface.

Also remember you can implement more than one interface per class, but can inherit from only one.  So if you wanted to have all three of those inherit from something that did not define the MakeNote function, then you could do that and ALSO implement the IWrite interface.
A C# interface is roughly equivalent to a C++ Abstract class with all pure methods and no data members. It is a way to specify a common message protocol without using inheritance.

David
If you want a hokey statement of the difference between implentation of an interface and inheriting:

If a implements b, a does b
If a inherits from b, a is-a b

David