The difference between an abstract class and an interface

Possible duplicate:
Interface vs Base class

I do not understand the difference between an abstract class and an interface. When do I need to use what type of art?

+3
source share
3 answers

When we create an interface , we basically create a set of methods without any implementation, which should be overridden by the implemented classes. The advantage is that it provides a way for the class to be part of two classes: one from the inheritance hierarchy and one from the interface.

, , , . , . - , , .

article along with the demo project discussed Interfaces versus Abstract classes.

+11

:

abstract "is-a". Volkswagon - .

"can-do". IDrive.

, IDrive, - .

+11

, , . ( ). , .

..

public abstract class A {
    public string sayHi() { return "hi"; } // a method with code in it
    public abstract string sayHello();  // no implementation
}

public class B 
   : A
{
    // must implement, since it is not abstract
    public override string sayHello() { return "Hello from B"; }
}

. , , ,. . .

public interface A
{
    string sayHi(); // no implementation (code) allowed
    string sayHello();  // no implementation (code) allowed
}

public class B
    : A
{
     // must implement both methods
    string sayHi() { return "hi"; }
    string sayHello() { return "hello"; }
}

, / ++. , ( ).

class A {
    virtual int a() = 0;  // pure virtual function (no implementation)
} 
+1
source

Source: https://habr.com/ru/post/1763066/


All Articles