What is the difference between an abstract class and an interface?

10,932

Solution 1

You can't instantiate an abstract class.

The difference between an abstract class and an interface is that an abstract class can have a default implementation of methods, so if you don't override them in a derived class, the abstract base class implementation is used. Interfaces cannot have any implementation.

Solution 2

Interfaces don't provide an implementation. You can also implement multiple interfaces.

You can provide an implementation inside of an abstract class, but you can only inherit from one base type.

In either case, you can't directly instantiate either one.

Solution 3

You cannot directly create an instance of an abstract class. You can, however, provide method and/or property implementations, which you cannot do in an interface. Also you can only inherit one class, abstract or otherwise, whereas you can inherit (implement) as many interfaces as you like.

abstract class A 
{
    public int Foo() { return 1; } // implementation defined
}

class B : A 
{
}

interface C
{
    int Foo() {return 1;} // not legal, cannot provide implementation in interface
}

// ... somewhere else in code

A a = new A(); // not legal
A a = new B(); // legal

Solution 4

Abstract Class:

  1. Abstract Class Can contain Abstract Methods and Non- Abstract Methods.

  2. When a Non-Abstract Class is Inherited from an Abstract Class, the Non-Abstract Class should provide all the implementations for the inherited Abstract Method.

Interface:

  1. Interface is nothing but Pure Abstract Class ie Interface can contain only the function declaration.

  2. All the members of the interface are Public by Default and you cannot provide any access modifiers.

  3. When a class is inherited from the interface, the inherited class should provide actual implementations for the inherited members.

Share:
10,932
Ali Mst
Author by

Ali Mst

I am an IT Software Architect from Salt Lake City, Utah.

Updated on June 04, 2022

Comments

  • Ali Mst
    Ali Mst almost 2 years

    Possible Duplicate:
    Interface vs Abstract Class (general OO)

    Can I ever instantiate an Abstract class? If so, why would I not make all my non-sealed classes abstract?

    If I can't instantiate it, then what is the difference from an interface? Can the abstract class have "base" class functionality? Is there more to the difference between an interface and an abstract class than that?