Declares an interface class. Interface classes are useful because they allow the developer to specify certain members of a class without defining what they do. For example, lets say you have a publishing software that can publish many different kinds of documents. You also have a class for each kind of document, and a PublicationManager. Now, you could write a different Publish() method in the PublicationManager for each type of document, but that is bad practice because you are tightly intertwining two separate functional systems. A better way would be to write an interface, IPublishable, with a Publish() method. There is nothing in the Publish() method, just the name of it. Now have each of your document classes implement (inherit) the IPublishable class. They now have an undefined Publish() method that needs to be defined, so you define each one as it needs to be, based on the differences in that class. Now you can call the Publish() method on any class, even if you don't know which one it is, all you have to know is that it implements IPublishable. So you can say:
public void SomeMethod(IPublishable doc)
{
doc.Publish();
}
And you can pass any class that implements IPublishable, without knowing which one specifically.
Interfaces are especially useful when you are writing software that will be extended by other users. All of your core functionality should define and use interfaces, so that other users can add new assemblies that just implement the interfaces.
2007-03-20 02:28:20
·
answer #1
·
answered by Rex M 6
·
1⤊
0⤋
Inheriting from a class is a powerful mechanism, but the real power of inheritance comes from inheriting from an interface. An interface allows you to completely separate the name of a method from its implementation.
An interface is a contract and defines the requisite behavior of generalization of types. For example, vehicle behavior includes ignition on, ignition off, turn left, turn right, accelerate, and decelerate. A car, truck, bus, and motorcycle are included in the vehicle category. As such, they must encapsulate baseline behavior representative of all vehicles. The vehicle interface defines that baseline. Specific vehicle types implement that baseline behavior differently than others. A car accelerates differently from a motorcycle. A motorcycle turns differently from a bus. An interface mandates a set of behaviors, but not the implementation. The derived type is free to implement the interface in an appropriate manner. Interfaces must be inherited. You cannot create an instance of an interface.
Any class or structure that inherits an interface commits to implementing the members of that interface. An interface is an array of related functions that must be implemented in a derived type. Members of an interface are implicitly public and abstract.
An interface is similar to an abstract class. Both types must be inherited. You cannot create an instance of either. Abstract members require implementation in the derived type. Interface members require implementation in a derived type
2007-03-20 09:54:09
·
answer #2
·
answered by Smutty 6
·
1⤊
0⤋
Interfaces are defined using the interface keyword. For example:
interface IComparable
{
int CompareTo(object obj);
}
Interfaces describe a group of related behaviors that can belong to any class or struct. Interfaces can be made up of methods, properties, events, indexers, or any combination of those four member types. An interface can not contain fields. Interfaces members are automatically public.
Classes and structs can inherit from interfaces similar to how classes can inherit a base class or struct, with two exceptions:
A class or struct can inherit more than one interface.
When a class or struct inherits an interface, it inherits member definitions but not implementations. For example:
public class Minivan : Car, IComparable
{
public int CompareTo(object obj)
{
//implementation of CompareTo
return 0; //if the Minivans are equal
}
}
2007-03-20 09:31:45
·
answer #3
·
answered by Milind 2
·
1⤊
1⤋
http://msdn2.microsoft.com/en-us/library/87d83y5b(VS.80).aspx
an INTERFACE specifies a "contract" that the "implementer" of the interface must comply with.
It says that the implementer must do certain things. But does not say how it must do them, or give any details other than what parameters go in and what must come out.
Functions and methods can be written to accept or return a variable of an interface type, and any variables of an interface type can be relied on to perform the interface methods.
For example
public interface IScreamer
{
void Scream();
}
public class Yeller : IScreamer
{
void IScreamer.Scream()
{
console.write("YELLLLLLL!!!!!!");
}
}
public class Shouter : IScreamer
{
void IScreamer.Scream()
{
console.write("SHOUT!!!!!!");
}
}
public class Farter : IScreamer
{
void IScreamer.Scream()
{
console.write("TBHTBHTHBHTHBHTHBHT!!!!!!");
}
}
class Sample
{
static void Main()
{
doScream(new Yeller()); //writes "YELLLLLLL!!!!!!"
doScream(new Shouter()); //writes "SHOUT!!!!!!"
doScream(new Farter()); // writes "TBHTBHTHBHTHBHTHBHT!!!!!!"
}
static void doScream(IScreamer screamer)
{
screamer.Scream();
}
}
2007-03-20 11:00:28
·
answer #4
·
answered by Anonymous
·
1⤊
0⤋
It is like an abstract class which is like a template.... you can have methods in it but for each method cannot be coded...
only when you implement an interface, you will have to code it... Used to standardise your programs or project to use the same method name etc.. works just like template
2007-03-20 09:30:47
·
answer #5
·
answered by mstq 3
·
1⤊
1⤋