Hi everyone, in this article I will explain what is abstract class & Interface, what their purpose and what is the difference between them.
Before starting I want to give a brief wind up on something like
1. Class: It is a collection of methods, attributes, Properties etc., all the methods in the class are concrete classes (definition already exists).
2. Concrete Methods: A method which contains the implementation.
Public void ConcreteMethod()
Console.WriteLine(“Method with Implementation");
3. Abstract Methods: opposite of abstract methods,i.e. method which does not have implementation or body.
Public void AbstarctMethod();
4. As we know both the abstract class and interface cannot be instantiated, because these are used for implementing the inheritance (which mean to derive), because of this we can use these by deriving in our class.
Definition: This is a class, which is a combination of both concrete and abstract methods (method without any definition)
A class which contains at least one abstract method
1. Can implement the common functionalities as concrete methods and non common functionalities as abstract methods in the same class.
2. Normal class + additional features of abstract methods.
Definition: This is a skeleton representation of a class. It is a pure abstract class.
1. This is used to interact between different modules to specify the methods and there signatures.
2. This is used to define the methods and the signatures types of the method while defining the skeleton, so that I will be easier to implement them.
3. This is used to force the user to implement all the methods declared in the interface.
4. For the implementation of multiple inheritance.
Difference between Interface and abstract class
1. As we see abstract class does not show full abstartion, but interface is a pure abstract class as it has only abstract methods.
2. Can implement the Multiple Inheritance with the use of interface.
3. The modifiers for methods in interface can be only public the modifiers for methods in abstract class can be public or protected.
4. One or more interfaces can be implemented only one abstract class can be extended
5. Only an interface can extend another interface, but class can extend an abstract class.
6. Abstract classes should have subclasses else that will be useless..Interfaces must have implementations by other classes else that will be useless
7. Accessibility modifier (Public/Private/internal) is allowed for abstract class. Interface doesn't allow accessibility modifier
8. An abstract class can contain fields, constructors, or destructors and implement properties. An interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.
1.Both the interface and the abstract class can contain zero or nil abstract methods.
2. In both cases all the abstract methods declared should be implemented/extended respectively
All the methods declared inside an Interface should be 'abstract', right ?
If correct, please explain something more about first point under Similarities section.
Well illustrated... Thanks Manoj
Difference between abstract class and interface?
*Abstract class can contain both abstract
methods and non abstract methods.
*If a class contains at least one abstract method
then that class must be declared as abstract
*Abstract class can inherit from another abstract
class and can inherit from more than one
*Abstract methods must be implemented in
*Object cannot be created for abstract classes,
reference can be created.
public abstract class myabstract: IA, IB //IA and
IB are two interfaces
public abstract viod m1(); //abstract method
public void m2() //non abstract method
//some logic .
*Interface contain only abstract methods, which
are public abstract by default.
*Interface can implement another interface but
cannot implement abstract class.
*Object cannot be created for interface, reference
can be created.
public interface ISample:IMysample
"Hope for the best.. Prepare for the worst.."
/// Abstract Class
An abstract class is a special kind of class that cannot be instantiated.
Because abstract class is only to be inherited by sub-classed (Child class ). In other words, it only allows other classes to be inherit but cannot be instantiated.
in Abstract class, child class must and should inherit the the parent class method and property.
A class can implement any number of interfaces but a subclass can at most use only one abstract class
An interface is also a type like class. It is an entity that is defined by the word Interface.
An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.
The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class.
Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.
Hope it will help for uu.
Thanks a lot,