Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call methods within a class that explicitly implements an interface?

Here's the story. I created an interface, IVehicle. I explicitly implemented the interface in my class, Vehicle.cs.

Here is my interface:

Interface IVehicle {         int getWheel(); } 

here is my class:

class Vehicle: IVehicle {       public int IVehicle.getWheel()      {          return wheel;      }       public void printWheel()      {          Console.WriteLine(getWheel());      } } 

Notice that getWheel() is explicitly implemented. Now, when I try to call that method within my Vehicle class, I receive an error indicating that getWheel() does not exist in the current context. Can someone help me understand what I am doing wrong?

like image 847
tyrone302 Avatar asked Mar 26 '10 01:03

tyrone302


People also ask

Can you call a method from an interface?

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.

Why we call it interface why not we can call it a class?

Difference between a class and an interface The basic difference is that a class has both a definition and an implementation whereas an interface only has a definition. Interfaces are actually implemented via a class.

Can a class not implement all methods of an interface?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.

Can we give method body in interface?

There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.


2 Answers

When you explicitly implement the interface, you first have to cast the object to the interface, then you can call the method. In other words, the method is only available when the method is invoked on the object as the interface type, not as the concrete type.

class Vehicle: IVehicle {       public int IVehicle.getWheel()      {          return wheel;      }       public void printWheel()      {          Console.WriteLine( ((IVehicle)this).getWheel() );      } } 

See this reference at MSDN for more information. Here's the relevant snippet:

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

For what it's worth -- this probably isn't a particularly good use of explicit interface implementation. Typically, you want to use explicit implementation when you have a class that has a full interface for typical operations but also implements an interface that may supersede some of those operations. The canonical example is a File class that implements IDisposable. It would have a Close() method but be required to implement Dispose(). When treating as a File you would use Open/Close. When opened in a using statement, however, it will treat it as an IDisposable and call Dispose. In this case Dispose simply calls Close. You wouldn't necessarily want to expose Dispose as part of the File implementation since the same behavior is available from Close.

like image 107
tvanfosson Avatar answered Oct 09 '22 02:10

tvanfosson


According to MSDN:

It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface.

And in the C# language specifications:

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

To access this member you can first cast the class to the interface, and then access it.

like image 29
Yuriy Faktorovich Avatar answered Oct 09 '22 03:10

Yuriy Faktorovich