I am reading/learning about interfaces in C# at the moment, and thus far I managed to understand how it differs from an abstract class. In the book I am reading the author explains that interfaces are the ultimate abstract class and that it simply sets the standard of certain methods the inheriting class will have, but then provides the following example.
static void Main(string[] args)
{
...
Circle c = new Circle("Lisa");
IPointy itPt = null;
try
{
itPt = (IPointy)c;
Console.WriteLine.(itPt.Points);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
...
}
The line that absolutely throws me off is the IPointy itfPt=null;
did he just declare an interface? I thought interfaces are abstract and can only be inherited? What kind of sorcery is going on here?
0
That example demonstrates a try…catch more than anything.
Interfaces are types too.. but they only expose their contract. So this:
IPointy itPt = new IPointy();
..won’t work. Consider this:
interface IPointy {
void MyMethod();
}
class Pencil : IPointy {
void MyMethod() {
}
void MyOtherMethod() {
}
}
You can declare an IPointy like this:
IPointy itPt = new Pencil();
..however, this will work:
itPt.MyMethod();
..this won’t work:
itPt.MyOtherMethod();
This is because MyMethod
is part of the IPointy contract.. MyOtherMethod
isn’t.. Hopefully that helps.
EDIT: Expanding this to explain my comment.
Inheriting from a class represents an “is-a” relationship. Implementing an interface represents a “can-do” relationship.
Consider:
interface ISwitchable {
void SwitchOn();
void SwitchOff();
}
class Light : ISwitchable {
void SwitchOn() { }
void SwitchOff() { }
}
class Television : ISwitchable {
void SwitchOn() { }
void SwitchOff() { }
}
Both a Television and Light inherit from ISwitchable.. which is fine. The interface defines a “can-do” relationship.. they can both be switched on orr of. Both of the following blocks of code are valid:
ISwitchable mySwitchableObject1 = new Light();
ISwitchable mySwitchableObject2 = new Television();
mySwitchableObject1.SwitchOn(); // calls Light's SwitchOn method
mySwitchableObject2.SwitchOn(); // calls Television's SwitchOn method
2
You can declare variables typed as interfaces: that does not amount to instantiating them. Once you declare a variable of interface type, you can assign it an object of any class that implements the interface.
For example, you can declare a variable of interface type IDictionary<string,string>
, but you cannot instantiate it: you must choose a class that implements IDictionary<string,string>
, for example
IDictionary<string,string> d = new Dictionary<string,string>();
or
IDictionary<string,string> d = new SortedList<string,string>();
An interface is declared using the interface keyword, which that line of code does not contain.
What you’re seeing is a variable declaration, a variable named itfPt, whose type is IPointy. The variable gets the value ‘null’, which is not an instance.
Instantiating the interface IPointy is not possible, you can try doing it by type itfPt = new IPointy(); and examining the compile errors. The only values that can be assigned to itfPt are instances of concrete subtypes of IPointy and null.
2
I think what you look for is instantiation of Interface and use of it. Here is an example that may help you:
public interface IFoo
{
void Bar1();
void Bar2();
}
public Class ActionableFoo : IFoo
{
Action _bar1, _bar2;
public ActionableFoo(Action b1, Action b2)
{
_bar1 = b1;
_bar2 = b2;
}
public void Bar1() { if(_bar1 != null) _bar1(); }
public void Bar2() { if(_bar2 != null) _bar2(); }
}
public void PushFoo(Action bar1, Action bar2)
{
IFoo foo = new ActionableFoo(bar1, bar2);
_fooStack.Push(foo);
}