Saturday, June 13, 2026

C# Basic Difference between Class and Interface

In this post we will see what is main purpose of class in object oriented programming and will compare class with interface.

We know that class is a user defined type but what is the real purpose of class. Class is basically used to hold complex data; just as we hold simple data using primitive data type, similarly, class is used to hold complex data type. For example, if we want to store the information of a student such as his name, age, address etc. then we will create a class named Student. Class is not only used to contain data but it also contain the logic to carry out operation on the data. These operations are called methods of the class. The other kind of functions which manipulate these data are properties, indexers, events, delegates etc. which can be members of a class. Therefore, a class encapsulates both data and functions.

Now we compare class with interface. Interface is a custom type which is used to contain abstract declaration of operations. These abstract operations can be in form of properties, methods, indexers and events. Suppose we want to define a set of operations as contract which can be implemented by another type then in this case, interface is appropriate type. Interface is implemented by class or struct. A class or struct can implement any number of interfaces. An interface can contain different kind of functions such as methods, properties, indexers etc. All these functions are by default implicitly public.They are public so that any class or struct could implement them.

Comparison between class and interface

1. A class can encapsulate both data and operations on these data. An interface encapsulates only operations. Data is not allowed in an interface.
2. The operations, also known as methods, properties, indexers, events can be in abstract or concrete form in a class but they are always abstract in interface. When an operation in class is abstract then the class is known as abstract class but all members of interface are abstract.
3. The methods, properties, indexers, events are by public default implicitly in interface. We cannot mark interface members with access modifier. But methods, properties, indexers, and events in a class can be marked with access modifier. It means that methods, properties, indexers, and events in a class can be public, private, protected etc.
4. A class can inherit another class or/and one or more interfaces. But an interface can inherit only other interfaces, not any class.
5. A class can inherit only one class but interface can inherit more than one interface.
6. A class represents objects but interface represents contract which must be implemented by class. An instance of class can be created using new operator but an interface cannot be instantiated.
7. By convention, both class and interface are named in Pascal case but interface name is also prefixed with a capital I. For example, Student is a class name but IComparer is an interface name.

No comments:

Post a Comment

Hot Topics