What is difference between class and interface in C#
Hi ,
I am a beginner in C# learning C# from wikitechy , i am not having a clear idea about class and interface.
Where and why i need to use interface ?
please clarify
Thanks in advance
Suresh Vijayan
Abstract Class | Interface |
---|---|
It contains both declaration and definition part. | It contains only a declaration part. |
Multiple inheritance is not achieved by abstract class. | Multiple inheritance is achieved by interface. |
It contain constructor. | It does not contain constructor. |
It can contain static members. | It does not contain static members. |
It can contain different types of access modifiers like public, private, protected etc. | It only contains public access modifier because everything in the interface is public. |
The performance of an abstract class is fast. | The performance of interface is slow because it requires time to search actual method in the corresponding class. |
It is used to implement the core identity of class. | It is used to implement peripheral abilities of class. |
A class can only use one abstract class. | A class can use multiple interface. |
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class. | If many implementations only share methods, then it is superior to use Interface. |
Abstract class can contain methods, fields, constants, etc. | Interface can only contain methods . |
It can be fully, partially or not implemented. | It should be fully implemented. |
As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class
A Class has both definition and an implementation whereas Interface only has a definition.
A Class can be instantiated but an Interface cannot be instantiated You can create an instance of an Object that implements the Interface.
A Class is a full body entity with members, methods along with there definition and implementation. An Interface is just a set of definition that you must implement in your Class inheriting that Interface.
class
A Class is a specification of how to construct Objects from the same Class . It is a type of blueprint or prototype from which individual Objects are created. We can say a Class is a template that describes the kinds of state and behavior that Objects of its type support.
interface:
it is one typeshow to create an Object, without caring about the specifics of how they do the things. An Interface is a reference type and it included only abstract members such as Events, Methods, Properties etc. and it has no implementations for any of its members.
Difference Between Interface and Abstract Class
- Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
- Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
- Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
- An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
- A Java class can implement multiple interfaces but it can extend only one abstract class.
- Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
- In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Interface:
An interface provides a contract
specifying how to talk to an object, but not the specifics of how that object handles that request (apart from parameters and return types etc)
Class:
A class (especially an abstract class) provides both the information on how to talk to an object, but in some cases the actual implementation (think overriding a method of a base class).
Example Program:
public interface ILog
{
void WriteMessage(string message);
}
public class ConsoleLogger : ILog
{
public void WriteMessage(string message)
{
Console.WriteLine(message);
}
}
public class MessageBoxLogger : ILog
{
public void WriteMessage(string message)
{
MessageBox.Show(message);
}
}
public void DoSomethingInteresting(ILog logger)
{
logger.WriteMessage("I'm doing something interesting!");
}
Because both ConsoleLogger
and MessageBoxLogger
implement the ILog
interface (the WriteMessage
method, any part of code can take an ILog
without ever needing to know what it actually does, they only care that it does something – in this case, writing a log message.
So in the code above, either a ConsoleLogger
or MessageBoxLogger
could be supplied to DoSomethingInteresting
it doesn’t matter at all because ILog
“knows” how to talk to that object.
An Interface can have only Abstract methods and Constants , which are always implicitly public, static, and final. A Class has both definition and an implementation whereas Interface only has a definition. … An Interface is just a set of definition that you must implement in your Class inheriting that Interface.
- 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.
example for class in c#
using System;
namespace MyShapes
{
class Shapes
{
public double length;
public double breadth;
int calArea()
{
int area = length*breadth;
}
}
………
}
example for interface in c#
namespace MyCalculator
{
interface Calculate
{
int add();
int subtract();
}
class CalculatorImplementer : Calculate
{
………..
int override add()
{
int a=10,b=5,c;
c=a+b;
}
int override subtract()
{
int a=20,b=10,c;
c=a-b;
}}
………
}
An interface is a contract: it specifies what members (methods and properties) a class implementing the interface must have. But because it is only a contract, it has no implementations for any of its members. A class can implement zero, one or multiple interfaces.
In contrast: a class is a… well… class of objects (like in taxonomy). For example, an Animal
is a class of living things, and a Giraffe
is a class of animals. Inheritance expresses this relationship: an Giraffe
is an Animal
when Giraffe
inherits from Animal
. It can do anything an animal can do, and more. It can provide implementations for its members, and in .NET a class will inherit from exactly one other class.
Class
Class is a user-defined data type that allows grouping of data members, methods, properties, and events. A class is nothing but a blueprint that defines data and behaviour. Objects are instances of 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.