• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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
0
paulwhelan
Asked:
paulwhelan
  • 4
  • 3
  • 2
  • +2
1 Solution
 
2266180Commented:
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 ;)
0
 
paulwhelanAuthor Commented:
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
0
 
2266180Commented:
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:)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrichmonCommented:
>>I know you can;t implement an interface so what can you do? And why use them?

You do implement an interface.  That means that you make your class conform to at the least the methods in defined by the interface.  Before I answer "Why use them" let me answer your next part

>>Also whats the difference between inheritance and an interface?

Inheritance is when one class extends another.  For example, you could have an animal class and then a cat class and a dog class.  Both the cat and dog class would inherit from the animal class.  Thus a cat IS A animal and a dog IS A animal.  That means anything you define in the animal class as protected or public will also be available in the child classes (cat and dog).  WIth inheritance you can define an actual function that works or you can define an abstract function - the child class must then define the body.

Syntax is
class Cat : Animal { ... }

An interface is only method declarations.  You don't define the body of a function in an interface, just the signature that must be implemented.  This is simply stating that if you implement it, you must then be sure to provide definitions for any methods listed in the interface.  

Syntax is:
class Pencil : Write { ... }
Notice the syntax looks the same.  Usually people name INterfaces with an I prefix so like this:

class Pencil : IWrite { ... }

However note that a Pencil does not meet the condition "IS A" Write.

You can have a single class implement multiple interfaces, but in C# a class is limited to inheriting from only one class.

>>Whats the point of interfaces?  And why use them?
In general you will implement pre-defined interfaces.  But you could do your own.  The point is that you are telling anyone who implements the interface that there is a common way to access certain functions.  You know that anything that implements that same interface will have a common way to be accessed.

So for example if I were writing a note taking class I could define classes
Email, Paper, StoneTablet

And an interface they all implement called Write which defines a function MakeNote

And as long as all of these implemented the Write interface that means that I know I could call the  MakeNote function on any of these and not worry that the Email class MakeNote function involves typeing, the Paper class MakeNote function involves finding an pen or pencil , and the StoneTablet class MakeNote requires a chisel and carving.

As a person using any of these classes, I do not need to know how they make a note, just that they all can.

Hope that helps
0
 
gbzhhuCommented:
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










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

Thanks
Paul
0
 
2266180Commented:
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?
0
 
paulwhelanAuthor Commented:
?

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
0
 
gbzhhuCommented:
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
0
 
mrichmonCommented:
>>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.
0
 
anyoneisCommented:
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
0
 
anyoneisCommented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now