Results 1 to 4 of 4

Thread: interface/inheritance

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: interface/inheritance

    awesome! That's just...fantastic. Thank-you so much

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width