Hi all ,
I ve'nt use an Interface yet as I am really confuse abt its use.
I have read in articles that When trying to build maintainable,
reusable, and flexible C# code, the object oriented nature of C# only
gets us 50% of the way there. Programming to interfaces can provide us with the last 50%.
Now if we go for Interface defination then :-
An interface is a reference type object with no implementation.
No properties or methods are actually coded in an interface, they are only defined.
So the interface doesn't actually do anything but only has a signature for interaction
with other classes or interfaces.
Can any one plz explain me how interface is the replacement of multiple inheritance of c++.
If it has only signature of the methods then how it helps for multiple inheritence.How
it increase code reusability.
If possible plz give some codes and help me understand this little concepts. I have went
through many articles but could'nt figure out where to use so that it will
increase COde reusabilty.
Thanks & Regards
s_raj0205
Loading
Rahul Kumar SaxenaPosted Jul 10, 2007, 4:14 AM
Class A: ClassB,Class c
This is wrong
But we can inherit more then one Interface
Class A: InterfaceB, InterfaceC
This Is right
suraj raiPosted Jul 10, 2007, 1:18 AM
saparate thread...
and still i feel i will be having lots of question even then....
Anyway..a lots n lots of thanks to u ..forums are realy a heaven for developers like us..
Thanks & Regards
s_raj0205
Posted Jul 9, 2007, 10:38 PM
http://www.java2s.com/Code/CSharp/Language-Basics/Interface.htm
Jan MontanoPosted Jul 9, 2007, 2:01 PM
Maybe you're looking reuse as reusing implemented code. It's not the case with interface. Actually you're just reusing the architecture or the design or the interface itself. IDomainObject consists of calls to my database. By inheriting from IDomainObject, I'm saying that this is a class for interfacing with my database. By inhering an abstract method Update, I can modify it's behaviour which suits the appropriate update command for my transaction. or my user table.
suraj raiPosted Jul 9, 2007, 9:44 AM
Here, is 'Transaction' class by any means related to 'User' class or
Interface 'DomainObject'.
It seems Transaction is a totaly independent class where you have delcared
Insert , update and delete function.
But as you ve reply for inteface ...is transaction class has anything to do with inteface
'DomainObject'.
And how this code will be help full for reusing Insert, Update and delete methods
in Interface.
Sory for killing ur valuable time...
Hoping 4 ur reply.
Thanks & Regards
s_raj0205
Jan MontanoPosted Jul 9, 2007, 8:11 AM
interface DomainObject
{
}
class Transaction
{
}
class User : DomainObject
{
}
suraj raiPosted Jul 9, 2007, 7:21 AM
Hi Jan,
I really appreciate the way you explain...tat was really a nice example
n help me to understand interface..
But stil i think the question of reusability remains there only as u said if I had to make
an Eagle class I will inherit IEagle inteface and had to implement the 2 properties
and the fly method again...
Can u give me some real time projects example ..like while doing some database application in windows
or any web application like ecommerce ..in such case wer we cn use Interface ...
I think using Interface in all kind of application is not the right way..
Your precios time will be heighly appreciated...Plz reply..
Thanks & regards
s_raj0205
Jan MontanoPosted Jul 9, 2007, 6:25 AM
Let's say I want to create a Gryphon Class. As a mythical creature, a gryphon is Half Lion and Half eagle. Fortunately I already have a design for my Lion Class and Eagle class. And the gryphon being a lion and eagle, I would want it to have the characteristics of a lion and an eagle. Not just the characteristics of either one of them, but both.
Here enters our interface. Instead of thinking of a way to design my gryphon class, I just simply reuse or inherit my interface for lion and eagle, then Voila! I can already implement my gryphon class. with no hassle.
interface IEagle
{
}
interface ILion
{
}
interface IGryphon : IEagle, ILion
{
}
class Gryphon : IGryphon
{
}
You'll notice that if you remove just one method or one property from the gryphon class, it will not compile, because techinally it inherits from ILion and IEagle, and as such, I am forced to implement all the behaviours of ILion and IEagle.
On the other hand, if I want to make an Eagle Class I just have it implement IEagle. And I'll know exactly what I have to put in code for my Eagle Class.
Hope this makes sense. Good luck. It took me half a day arriving with this idea. hahaha! (^_^)