1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What’s the difference between an interface and abstract class?

Discussion in 'C#' started by alendonald, Jul 9, 2013.

  1. #1
    Hi,
    I am little bit confusion between interface and abstract class. So tell me the difference between in both.
    SEMrush
     
    alendonald, Jul 9, 2013 IP
    SEMrush
  2. GMF

    GMF Well-Known Member

    Messages:
    856
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #2
    This site should clear your confusion
    http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
    Code (markup):
     
    GMF, Jul 10, 2013 IP
  3. vihutuo

    vihutuo Well-Known Member

    Messages:
    1,511
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    130
    #3
    An abstract class is like any other class, the only difference is that it cannot be instantiated, you need to inherit from it. An interface cannot contain any code only declarations of methods. In C# there is no multiple inheritance, each class can have only one superclass but a class can implement multiple interfaces
     
    vihutuo, Jul 11, 2013 IP
  4. jamdp

    jamdp Active Member

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    • Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods stubs.
    • Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public.
    • When inheriting an abstract class, the child class must define the abstract methods, whereas an interface can extend another interface and methods don't have to be defined.
    • A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.
    • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.
     
    jamdp, Jul 23, 2013 IP
  5. alendonald

    alendonald Greenhorn

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #5
    Thanks jamp dp for nice information.
     
    alendonald, Jul 25, 2013 IP
  6. David Polski

    David Polski Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    Digital Goods:
    1
    #6
    Thanks for imformation :)
     
    David Polski, Jul 25, 2013 IP
  7. gym.prathap

    gym.prathap Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    1. A class can implement more than one interface(Multiple Inheritance is possible with interface). But A class can not extend more than one abstract class(Multiple Inheritance is not possible with class).

    2. If You want to add a new method in interface, you need to implement that method in all the classes that implementing the interface. But if you want to add a new method in abstract class, you can add it as a non abstract method. You don't need to implement it in all the classes that extending the abstract class.

    3. Interface don't have constructors. But Abstract class has constructors.
     
    gym.prathap, Aug 12, 2013 IP
  8. jamieellis

    jamieellis Active Member

    Messages:
    427
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Use abstract classes if you have classes that will share a lot of common functionality (the functionality will be the same). Use interfaces if the interface is the same but the behavior is very different.

    If you had Lorry and Car with one method move() you might choose to use an abstract class Vehicle and implement move() in the abstract class since the functionality will be the same for both Lorry and Car.

    If you had Lorry and Plane an interface would be better since move() will be implemented differently.

    The benefit of this you can have a single method doMove(Vehicle v) that accepts car, Lorry or Plane and can invoke move() without caring about implementation details. This is an example of polymorphism.
     
    jamieellis, Aug 23, 2013 IP
  9. Alisha John

    Alisha John Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #9
    Suppose we have two methods M1() and M2() in an interface. An abstract class also has just the same two abstract methods. If any class implemented this interface or inherited from the abstract class, it will have to implement both the methods in it.


    So to me, it seems that an interface or an abstract class behaves the same for my scenario. So, can anyone highlight the difference between these two in this specific case and suggest whether to use an abstract class or an interface here?
     
    Alisha John, Sep 19, 2013 IP
  10. dellmerca

    dellmerca Banned

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #10
    An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
    An interface is an empty shell, just only the signatures of the methods. The methods do not contain anything. The interface can't do anything. It's just a pattern. An Abstract class is a class which will contains both definition and implementation in it.

    Maore about Abstract class and Interface...http://net-informations.com/faq/net/abstract.htm

    Dell
     
    dellmerca, Jan 19, 2015 IP
  11. Core I-Solutions

    Core I-Solutions Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #11
    Interfaces

    An interface is a contract: the guy writing the interface says, "hey, I accept things looking that way", and the guy using the interface says "Ok, the class I write looks that way".

    An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern.

    E.G (pseudo code):

    // I say all motor vehicles should look like this:
    interface MotorVehicle
    {
    void run();

    int getFuel();
    }

    // my team mate complies and writes vehicle looking that way
    class Car implements MotorVehicle
    {

    int fuel;

    void run()
    {
    print("Wrroooooooom");
    }


    int getFuel()
    {
    return this.fuel;
    }
    }
    Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there is no expensive look-up to do. It's great when it matters such as in embedded devices.

    Abstract classes

    Abstract classes, unlike interfaces, are classes. They are more expensive to use because there is a look-up to do when you inherit from them.

    Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It's more about a guy saying, "these classes should look like that, and they have that in common, so fill in the blanks!".

    e.g:

    // I say all motor vehicles should look like this :
    abstract class MotorVehicle
    {

    int fuel;

    // they ALL have fuel, so why not let others implement this?
    // let's make it for everybody
    int getFuel()
    {
    return this.fuel;
    }

    // that can be very different, force them to provide their
    // implementation
    abstract void run();


    }

    // my team mate complies and writes vehicle looking that way
    class Car extends MotorVehicle
    {
    void run()
    {
    print("Wrroooooooom");
    }
    }
     
    Core I-Solutions, May 2, 2015 IP
  12. zinist

    zinist Banned

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    43
    #12
    DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE:

    1. An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
    2. Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
    3. We can not declare a member field in an Interface.
    4. We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
    5. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed
     
    zinist, Jun 26, 2015 IP
  13. jackycheung01

    jackycheung01 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    - Abstract classes can have implementations for some of its members (Methods) but the Interface can’t have implementation for any of its members.
    - Interfaces cannot have fields but an abstract class can have fields.
    - An interface can inherit another interface only and cannot inherit from an abstract class but an abstract class can inherit from another abstract class or another interface.
    - A class can inherit from multiple interfaces at the same time, whereas a class cannot inherit from multiple classes at the same time.
    - Abstract class member can have access modifiers but interface members cannot have access modifiers (Public).
    - Abstract class can have constructor but interface cannot.
     
    jackycheung01, Mar 9, 2017 IP