Sunday, July 12, 2009

Why interfaces are needed in java or C# ?

Hi,





we know interface is like a skeleton, that contains only method signatures and variables. Methods must be public, abstract and variables must be public static final.





simply we are implementing interface and defining all the methods in our class. we can do this without going to interface.





why we have this interface? please explain


thank u........

Why interfaces are needed in java or C# ?
There are many reasons for keeping the interface and definitions separate. The two most important that come to mind are organization and support for circular dependencies.





The interface is the bare minimum needed to explain what a function or class will do. It's the lease amount of code you can write for a full declaration of functionallity and useability. For this reason, it's (more) clear for a user of your function or class to understand how the object works. The user shouldn't have to look through all of your implementations to understand what the object does. So, providing the interface separate, is a more organized approach.





Sometimes (although, it's considered bad programming practice) you need to have two or more objects which will require instances or use of the others and vice verca. For example: Objects A and B are defined as A is an object which contains one B. B is an object which contains one A. Without an interface signature before the definition, it is very difficult to implement this relationship.
Reply:I'm guessing that interface is a way to let multiple inheritance happen.
Reply:Below are 2 interface articles I've just been reading. Coincidence how you're asking about them as well! Although they're in PHP, the same theory applies.


No comments:

Post a Comment