Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

I would strongly suggest reading the MSDN help or getting a good book on interfaces (and object oriented technology in general). These forums are better suited to getting answers to specific questions.

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

The idea behind interfaces is this - standardisation!

 

Basicaly, you make a "layout" or 'Stracture" of a class, and anyone who wants t use it has to follow your stracture.

 

This dosn't have an actual function, but it is good when trying to build a big application, and you need all the programmer to follow certain stractures.

 

But that is just sratching the surface - read, read, read!

Latly it would seem as though I'm don't abnegate from anything... except women. :( :)
Posted

This explains it to me. Light bulb is turning on in my head. I now got it.

 

Interfaces are a powerful programming tool because they allow you to separate the definition of objects from their implementation, allowing your objects to evolve without risk of breaking existing applications. Both interfaces and class inheritance each have advantages and disadvantages, and you may end up using a combination of both in your projects. This topic and When to Use Inheritance will help you determine which approach is best for your situation.

 

Safe Changes to Code

Interfaces eliminate a significant problem of class inheritance: the likelihood of breaking code when you make post-implementation changes to your design. Even though class inheritance allows your classes to inherit implementation from a base class, it also forces you to make most of your design decisions when the class is first published. If your original assumptions turn out to be incorrect, you cannot always change your code safely in later versions.

 

For instance, suppose you define a method of a base class that expects an Integer argument, and then later determine that the argument should be of the Long data type. You cannot safely change the original class, because applications designed for classes derived from the original may not compile correctly. This problem can be magnified because a single base class can affect hundreds of subclasses.

 

One solution is to define a new method that overloads the original and that takes an argument of type Long. However, this might not be satisfactory because a derived class may need to override the method that takes the integer, and may not function properly if the method that takes a Long is not overridden also. Interfaces solve this problem by allowing you to publish an updated interface that accepts the new data type.

 

Flexibility in Implementation

There are several other reasons why you might want to use interfaces instead of class inheritance:

 

Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.

Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.

Interfaces are better in situations in which you do not need to inherit implementation from a base class.

Interfaces are useful in cases where you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...