You must Sign In to post a response.

OOPS - Why we need an Interface?

The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
This thread will be reviewed shortly.
If you think this thread contain inappropriate content, please report to webmaster.
I know all about an Interface, but I want to know where and when to use it?

For example,

/// Interface demo
Interface IDemo
{
// Function prototype
public void Show();
}

// First class using the interface
Class MyClass1 : IDemo
{
public void show()
{
// Function body comes here
Response.Write("I'm in MyClass");
}
}

// Second class using the interface
Class MyClass2 : IDemo
{
public void show()
{
// Function body comes here
Response.Write("I'm in MyClass2");
Response.Write("So, what?");
}
}

This two classes has the same function name with different body.
This can be even achieved without Interface.
Then why we need an Interface where and when to use it.

Some good explantions will be helpful.


Comments

Author: Vipin S19 May 2005 Member Level: Gold   Points : 2

This can be even achieved without Interface..yes correct...but when you implemnt interface u are very much sure ,that the method in the interface is implemented in the class.

means when u implement interface u need to have all the method defined in the interface needs to be implemented in the class.,eventhough the implementation will be different.

so..interface is a contract ...

Author: HimaBindu Vejella19 May 2005 Member Level: Gold   Points : 2

Its a good question!!
Interface is a class which contain all unimplemented methods( can call them also abstract methods)
We will implement these methods in derived class of interface.
ie an interface is actually not inherited but implemented.
We cann't create an instance of interface object instead we can create an instance for derived class objects...(I think this u know)
If the requirement is like that something in your design changes frequently then go for interfaces instead of classes

For example if you want to your project should support different databases . so that client can change his database in future we use interfaces contains property procedures in class file with out altering objects

Here is another example, the Strategy pattern lets you swap new algorithms and processes into your program without altering the objects that use them. A media player might know how to play CDs, MP3s, and wav files. Of course, you don't want to hardcode those playback algorithms into the player; that will make it difficult to add a new format like AVI. Furthermore, your code will be littered with useless case statements. And to add insult to injury, you will need to update those case statements each time you add a new algorithm. All in all, this is not a very object-oriented way to program.

With the Strategy pattern, you can simply encapsulate the algorithm behind an object. If you do that, you can provide new media plug-ins at any time. Let's call the plug-in class MediaStrategy. That object would have one method: playStream(Stream s). So to add a new algorithm, we simply extend our algorithm class. Now, when the program encounters the new media type, it simply delegates the playing of the stream to our media strategy. Of course, you'll need some plumbing to properly instantiate the algorithm strategies you will need.

This is an excellent place to use an interface. Thus, you should define the strategy as an interface. You should generally favor interfaces over inheritance when you want an object to have a certain type; in this case, MediaStrategy. Relying on inheritance for type identity is dangerous; it locks you into a particular inheritance hierarchy. C# doesn't allow multiple inheritance, so you can't extend something that gives you a useful implementation or more type identity
So we use interfaces

Author: Sarvanan09 Apr 2007 Member Level: Bronze   Points : 2

Madam can you explain with another example for the same question

Author: Sarvanan09 Apr 2007 Member Level: Bronze   Points : 2

Madam can you explain with another example for the same question

Author: Manikandan.J13 Jun 2007 Member Level: Bronze   Points : 2

good question!

why we go for on interface is simple.interface is just like a component reusabilty, just assume this example in programing level, if you creating a project for car manufacturing. just consider the impartant components might be placed in interface ( which are needs to run a car ). In your application you implement all the functionality of ur interface.This same methods can be use in other program. So the interface contain minimum line of coding and very easy to understandable by other developer and easy to explain to your client.

Author: sethuraman30 Dec 2007 Member Level: Bronze   Points : 2

please give code wise example for different database example

Author: Mrs. Meetu Choudhary Nanda23 Oct 2008 Member Level: Gold   Points : 6

Hi,

Interface

A standard way of calling routines and passing data structures. For example, the interface between two layers of code might be expressed in terms of routines that pass and return a particular data structure. Linux's VFS is a good example of an interface.

An interface in the programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final). An interface may never contain method definitions.

As interfaces are implicitly abstract, they cannot be directly instantiated. Object references may be specified to be of an interface type; in which case they must either be null, or be bound to an object which implements the interface.

The keyword implements is used to declare that a given class implements an interface. A class which implements an interface must either implement all methods in the interface, or be an abstract class.

One benefit of using interfaces is that they simulate multiple inheritance.


for more information please visit

http://en.wikipedia.org/wiki/Interface_(Java)

==
Thanks and Regards
Meetu Choudhary