Class
Class is a user-defined data type that allows the grouping of data members, methods, properties, and events. A class is nothing but a blueprint that defines data and behaviour. Objects are instances of the class. In C#, classes are defined using the ‘class’ keyword followed by the class name that contains the body of a class surrounded by curly braces. Every class has a constructor that has the same name as the class and called automatically at the time of instantiating a class.
Interface
An Interface contains only the signature of members: methods, properties, events or indexers. It does not contain the definition of these members. It is up to the class which is deriving this interface to write the definition of these members. It is mandatory for a class to implement all the members of the interface.
Example of Interface in C#:
In the above example, class CalculatorImplementer is implementing method add() and subtract() declared in the Calculate interface.
Difference Between Interface and Class
- A class can contain data members and methods with the complete definition. An interface contains the only signature of members.
- A class can only be inherited from a single class but can be inherited from more than one interfaces.
- Interfaces are always implemented whereas classes are extended.
- Classes represent the “real object” and do all the work. Interfaces allow you to create a program that will manipulate the class in a predefined way.
Leave a Reply