Difference between class and interface

asp.net India
  • 14 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

  • 14 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

  • 14 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.


























  • 14 years ago

    hi friends

    Thanks a lot for reply..

  • 11 years 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

  • 8 years ago

    Hi Friends,

    Thanks for sharing your confusion.

    Following are the difference between the Classes and Interface

    1. The interfaces are used in java to implementing the concept of multiple inheritance whereas classes are not used to implement multiple inheriyence.

    2. We are not allocating the memory for the interfaces whereas the memory is allocated for the classes

    3. Interfaces are always implemented whereas classes are always extended

  • 8 years ago

    Hi Friends,

    Thanks for sharing your confusion.

    Following are the difference between the Classes and Interface

    1. The interfaces are used in java to implementing the concept of multiple inheritance whereas classes are not used to implement multiple inheriyence.

    2. We are not allocating the memory for the interfaces whereas the memory is allocated for the classes

    3. Interfaces are always implemented whereas classes are always extended

Post a reply

Enter your message below

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”