Interfaces
An interface defines a set of methods that will be implemented by a class. An interface does not, itself, implement any method. Thus, an interface is a purely logical construct that describes functionality without specifying implementation.
interface ICalculator
{
void Add();
double Divide();
string ShowResult();
}
In object-oriented programming it is sometimes helpful to define what a class must do, but not how it will do it. Interfaces are syntactically similar to abstract classes. However, in an interface, no method can include a body. That is, an interface provides no implementation whatsoever. It specifies what must be done, but not how.
class Test : ICalculator
{
public void Add()
{
throw new NotImplementedException();
}
public double Divide()
{
throw new NotImplementedException();
}
public string ShowResult()
{
throw new NotImplementedException();
}
}
Once an interface is defined, any number of classes can implement it. Also, one class can implement any number of interfaces. Where as one class can inherit only one class or abstract class.
class Test : ICalculator
{
public void Add()
{
throw new NotImplementedException();
}
public double Divide()
{
throw new NotImplementedException();
}
public string ShowResult()
{
throw new NotImplementedException();
}
}
class Test2 : ICalculator
{
public void Add()
{
throw new NotImplementedException();
}
public double Divide()
{
throw new NotImplementedException();
}
public string ShowResult()
{
throw new NotImplementedException();
}
}
To implement an interface, a class must provide bodies (implementations) for the methods described by the interface. Each class is free to determine the details of its own implementation. Thus, two classes might implement the same interface in different ways, but each class still supports the same set of methods. Therefore, code that has knowledge of the interface can use objects of either class since the interface to those objects is the same. By providing the interface, C# allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism.
class Test : ICalculator
{
public void Add()
{
Console.WriteLine("Implemeting Add Method In Test Class");
}
public double Divide()
{
return 0.0;
}
public string ShowResult()
{
return "Implemeting ShowResult Method In Test Class";
}
}
class Test2 : ICalculator
{
public void Add()
{
throw new NotImplementedException();
}
public double Divide()
{
throw new NotImplementedException();
}
public string ShowResult()
{
throw new NotImplementedException();
}
}
In an interface, methods are implicitly public, and no explicit access specifier is allowed. This is because interfaces are made for implemented by other classes. If methods are not public it will be impossible to implement the interfaces by other classes.
interface ICalculator
{
public void Add();
double Divide();
string ShowResult();
}
Code
|
Description
|
File
|
CS0106
|
The modifier 'public' is not valid for this item
|
E:\inheritance Examples\inheritance Examples\Program.cs
|
Interfaces can specify properties, indexers, and events.
interface ICalculator
{
int x { get; set; }
int y { get; set; }
void Add();
double Divide();
string ShowResult();
}
class Test : ICalculator
{
int valX;
public int x
{
get { return valX; }
set { valX = value; }
}
int valY;
public int y
{
get { return valY; }
set { valY = value; }
}
public void Add()
{
Console.WriteLine("Implemeting Add Method In Test Class");
}
public double Divide()
{
return 0.0;
}
public string ShowResult()
{
return "Implemeting ShowResult Method In Test Class";
}
}
Interfaces cannot have data members.
interface ICalculator
{
int No;
int x { get; set; }
int y { get; set; }
void Add();
double Divide();
string ShowResult();
}
Code
|
Description
|
File
|
CS0525
|
Interfaces cannot contain fields
|
E:\inheritance Examples\inheritance Examples\Program.cs
|
Interfaces cannot define constructors, destructors, or operator methods. Also, no member can be declared as static inside an interface.
Once an interface has been defined, one or more classes can implement that interface. To implement an interface, the name of the interface is specified after the class name in just the same way that a base class is specified.
When a class implements an interface, the class must implement the entire interface. It cannot pick and choose which parts to implement. So always design your interface small and keep those methods only which are relevant to it.
A class can implement more than one interface. When a class implements more than one interface, specify each interface in a comma-separated list.
A class can implement a base class along with interfaces. In this case keep your base class first followed by your interfaces with a comma separated.
The methods that implement an interface must be declared public in the implemented class. The reason for this is that methods are implicitly public within an interface, so their implementation must also be public in the implemented class.
The return type and signature of the implementing method must match exactly the return type and signature specified in the interface definition.
Like abstract classes, we can not create any object for the interface as they are made for implemented by other class and are incomplete like abstract classes.
One interface can inherit other interfaces like classes. And when inherited interface is implemented by a class you need to implement all the base interface and derived interface methods defined in the interface.
All rules applied for class interface are applicable for interface inheritance. So name Hiding with Interface Inheritance is allowed. When one interface inherits another, it is possible to declare a member in the derived interface that hides one defined by the base interface. This happens when a member in a derived interface has the same declaration as one in the base interface. In this case, the base interface name is hidden. This will cause a warning message unless you specify the derived interface member with new.
When implementing a member of an interface, it is possible to fully qualify its name with its interface name. Doing this creates an explicit interface member implementation, or explicit implementation
There are two reasons that you might need to create an explicit implementation of an interface method. First, when you implement an interface method using its fully qualified name, you are providing an implementation that cannot be accessed through an object of the class. Instead, it must be accessed via an interface reference. Thus, an explicit implementation gives you a way to implement an interface method so that it is not a public member of the class that provides the implementation. Second, it is possible for a class to implement two interfaces, both of which declare methods by the same name and type signature. Qualifying the names with their interfaces removes the ambiguity from this situation. Let’s look at an example of each.
When to Choosing Interface over Abstract Class: When you can fully describe the concept in terms of “what it does” without needing to specify any “how it does it,” then you should use an interface. If you need to include some implementation details, then you will need to represent your concept in an abstract class.
No comments:
Post a Comment