Showing posts with label abstract classes and interfaces. Show all posts
Showing posts with label abstract classes and interfaces. Show all posts

Feb 11, 2010

Difference between abstract classes and interfaces in C#

Abstract Classes

- In abstract class a method must be declared as abstract
- For abstract methods there will be no body
- The Abstract methods can delare with Access modifiers(public,internal,protected etc)But when implementing in subclass same access modifier we have to use.
- The Abstract class can contain variables and concrete methods
- Multiple inheritance is not possible
- must and should call all the abstract methods in the derived class
- To implement abstract method in derived class we have to use override keyword
- The accessmodifiers and return types(like void,int etc) must be same in abstract method and in derived class method
- we cannot create an object to abstract classes


Interfaces

- For interface methods also there will be no body
- In Interfaces we cannot use any access modifiers.By default these methods are public.
- We cannot declare variables and concrete methods in interfaces
- Multiple inheritance is possible
- must and should call all the interface methods in the derived class
- To implement interface methods in derived class no need of override keyword
- The accessmodifiers and return types(like void,int etc) must be same in interface methods and in derived class methods
- we cannot create an object to interfaces