|
-
Oct 21st, 2006, 04:43 PM
#1
Thread Starter
PowerPoster
interface/inheritance
Just want to increase my knowledge and for best practices.
I think I know what an interface is. But can someone explain to me, with a reasonable example too (And code snips) on:
What is an interface? how would you implement one in your app? Why would you want to do this?
What is inheritance? how would you implement this in your app? Why do you want to do this?
Would very much appreciate it!
-
Oct 21st, 2006, 05:11 PM
#2
Re: interface/inheritance
Interface
An Interface is a reference type and it contains only abstract members. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them.
Link 1 on Interface
A small example on Interface
Inheritance
Link 1
Example on Inheritance
Differences
Interface is a data-type while Inheritance is a concept. When we say inheritance, in a broader sense it means, we are inheriting a class.
Interfaces are used when we want to inherit more than 1 class. Because Multiple Inheritance (again, of classes) are not allowed, we create Interfaces to accomplish this goal.
-
Oct 21st, 2006, 07:14 PM
#3
Re: interface/inheritance
When you inherit a class you are saying that an instance of the derived class IS an instance of the base class, but more specialised. When you implement an interface you are saying that an instance of the class CAN DO what the interface does. A derived class receives every member of its base class for nothing. By contrast, an interface contains no functionality. It merely says that any class that implements it will provide a certain set of functionality.
Think of your computer and its USB ports. The USB specification is an interface. There are gazillions of peripherals that implement the USB interface and you can plug any one of them into a USB port and it will work. That's because your computer knows the USB interface too and it knows that any peripheral that is plugged into a USB port will implement that interface. Thos eperipherals will provide other functionality to, but the USB controller doesn't care about that. It's job is just to interact with the properties and members of the USB interface.
Programming interfaces are the same. A great example is the IList and IListSource interfaces. Controls with a DataSource property can be bound to absolutely any object that implements one of those interfaces. It doesn't matter at all what other functionality the object provides or how it's structured internally, as long as it implements one of those interfaces it can be bound to a control.
As for inheritance, you already know when to use that and why it's a good thing. Imagine if you had to write the code for your forms from scratch every time. Every form you design inherits the Form class, so you get bucketloads of functionality for free right away. You can do the same with your own classes. Create a class that contains a load of functionality, then create other classes that inherit that class and thus inherit all that functionality, then add new functionality to each of them.
-
Oct 21st, 2006, 07:27 PM
#4
Thread Starter
PowerPoster
Re: interface/inheritance
awesome! That's just...fantastic. Thank-you so much
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|