Understanding Interfaces

Post on 23-Dec-2014

306 views 1 download

Tags:

description

Understanding Interfaces, Explicit Interface, Interface Implimentation

Transcript of Understanding Interfaces

Understanding InterfacesBhushan Mulmule

bhushan.mulmule@gmail.comwww.dotnetvideotutorial.com

Interfaces are simplest construct of C#. But understanding when and why to use them can take years…

www.dotnetvideotutorial.com

In this session let us focus on understanding interface as a C# construct.

We will discuss one of its usability in next session “Dependency Injection” where we will see how we can create loosely coupled

systems using interfaces

MSDN Defines Interfaces as…

An interface contains only the signatures of methods,  properties, events or indexers.

A class or struct that implements the interface must implement the members of the

interface that are specified in the interface definition.

Let us try to understand using simple example.Let us create simple interface IDemoInterface

with two methods defined in it…

www.dotnetvideotutorial.com

By default every member of interface is public so interface

doesn‘t allow use of access modifier.

Interface doesn't have any implementation.

We have to implement members defined by interface

in class that we will drive from interface.

That’s why we never say that “we are deriving class from interface”

Instead we say that “we are implementing interface in class”

Let us implement IDemoInterface in DemoClass

www.dotnetvideotutorial.com

Interface implementation syntax is same as class inheritance.

We need to implement all methods of IDemoInterface in DemoClass

Now we have two ways to create object of DemoClass.

Normal Instantiation: Using reference of DemoClass only

Or

www.dotnetvideotutorial.com

Upcasting: Using reference of IDemoInterface

And in both the cases output will be same...

Now let us add little complexity to our example.

Assume that there is one more interface ISampleInterface with two methods…

www.dotnetvideotutorial.com

This interface have two methods fun2() and fun3().

We have intentionally kept fun2() in both the interfaces .

C# allows multiple interface implementation.

Let us implement both the interfaces in DemoClass

Imp Note: Multiple class inheritance is not allowed in C#. i.e. we can’t derive class from two or more classes

Now we have three ways to create object of DemoClass

www.dotnetvideotutorial.com

or

or

First let us have normal instantiation…

Output:

www.dotnetvideotutorial.com

But in this case we have assumed that fun2() of both the interfaces has common implementation.

Go back and check in DemoClass we have single implementation of fun2()

What if we wanted to provide separate implementations for fun2() of IDemoInterface and

ISampleInterface?

www.dotnetvideotutorial.com

We can do it using Explicit Interface Implementation

Now fun2() is explicitly implemented and have two separate implementations.

Also observe that explicitly implemented methods can’t have access modifier in

DemoClass as they will be always public

We can’t call explicitly implemented methods using

reference of class.

www.dotnetvideotutorial.com

We can call only fun1() and fun3() using reference of DemoClass. Compiler will not able to resolve method call to fun2() as there are

two implementation on fun2() in DemoClass.

Million dollar question How to call fun2()?

We can call explicitly implemented methods using

reference of Interface.

So to call fun2() of IDemoInterface we can create object of DemoClass using reference of

IDemoInterface

www.dotnetvideotutorial.com

Interface reference has access to all its methods.

Output:

Same way to call fun2() of ISampleInterface we can create object of DemoClass using reference of ISampleIneterface

www.dotnetvideotutorial.com

Output:

Note that Interface reference has access to its

own methods only. Reference of one interface

can’t call method of other interface.

Is there any way out to call explicitly implemented

method using reference of class?

Yes!We can typecast reference of class to interface

www.dotnetvideotutorial.com

Here we are typecasting reference of DemoClass ; first to IDemoInterface

and then to ISampleInterface.

Output:

We can also derive one interface from other..

www.dotnetvideotutorial.com

Here IDerivedInterface is derived from IBaseInterface

So we have to implement methods of both the interfaces in class which implements IDerivedInterface

In next session we will have look on how to use interfaces to create loosely

coupled systems.

www.dotnetvideotutorial.com

For video visit www.dotnetvideotutorial.com

Bhushan Mulmulebhushan.mulmule@dotnetvideotutorial.com

www.dotnetvideotutorial.com