Register now or log in to join your professional community.
1. An Abstract class is a class which contains zero or more abstract members(methods, properties, event, and indexer).
2. It is mandatory to give implementation to all the abstract members in the derived class when you are inheriting your class from abstract class otherwise you need to declare your class as abstract class.
3. You need to create an abstract class when you have a family of related classes which share some common behavior and some uncommon behavior.
1. An interface is a group of members with empty bodies.
2. Any non-abstract type inheriting the interface must implement all its members.
3. Within interface no access modifier is allowed and by default access modifier is pulic.
4. When we have series of unreleted classes which is not having any common behavior, it is better to implement interface.
Abstract Class : We can not create an object of an abstract class and can call the method of abstract class with the help of class name only.
7.In C# , an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.
An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.
An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class sub-classing from it, it is said to implement that interface.
Example for abstract class:
public abstract class Shape
{
public abstract void draw();
}
Example for interface class:
public interface Drawable
{
void draw();
}
I will try to describe my answer very briefly.
Interface can't include any implementation. An interface is a set of method signatures (of course, interface can also define parameters and events). Also, you can inherit your class from several interfaces at the same time.
Abstract class can include behaviors which have an interface. But you can inherit your class from only one abstract class (a class is always derived from only one class). You are able to mark abstract methods as public, internal and protected. But interface's methods can be marked only public.
In my opinion, it's main differences.
The most important difference between Interfaces and Abstract Classes is the semantic difference.
An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is.
Technical differences between Abstract Classes and Interfaces is that an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes. A class can implement multiple interfaces, but it can only inherit one class (abstract or otherwise).
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".
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