Community discussion forum

Difference between class and interface

Tags: asp.net India
  • 3 years ago

    Hi all

    I am bit of confused about interface.

    Please explain me what is an interface and how it differs from a class?

    thanks

    Regards

    Anil Rayal

  • 3 years ago

    I'm no expert but in simple terms an Interface defines what an object can do a Class defines how it is done, so the two are seperated.

    Regards

    Brin

  • 3 years ago

    Hi,

    You can not create an instance of an interface, hou have to create an instance of an object that implements the interface. With an interface you just define what an object can/must be able to do. You will use Interfaces when you want to use various objects in the same way.


    Sample:
    Create an interface for a vehicle called IVehicle
    This interface will have the property Wheels.
    Create an object Car which implements the interface Ivehicle.
    Car.Wheels will alwais return 4
    2Create an object Bike with implements the interface Ivehicle.
    Bike.Wheels will alwais return 2
    What you can then do is:
    IVehicle vehicle = new Car();
    if you use vehicle.Wheels then it will return 4
    If you then do a:
    vehicle = new Bike();
    Now vehicle.Wheels will return 2.


























  • 3 years ago

    hi friends

    Thanks a lot for reply..

  • 5 months ago

    In general, interfaces are used only in languages that use the single-inheritance class model. In these single-inheritance languages, interfaces are typically used if any class could use a particular method or set of methods. Also in these single-inheritance languages, abstract classes are used to either have defined class variables in addition to none or more methods, or to exploit the single-inheritance model to limit the range of classes that could use a set of methods.

    Languages that support the multiple-inheritance model tend to use only classes or abstract base classes and not interfaces. Since Python supports multiple inheritance, it does not use interfaces and you would want to use base classes or abstract base classes http://www.marblehost.com/uptime-guarantee.php Link Text

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback