understanding interfaces

52
Understanding Interfaces Bhushan Mulmule [email protected] www.dotnetvideotutorial.com

Upload: bhushan-mulmule

Post on 23-Dec-2014

306 views

Category:

Technology


1 download

DESCRIPTION

Understanding Interfaces, Explicit Interface, Interface Implimentation

TRANSCRIPT

Page 1: Understanding Interfaces

Understanding InterfacesBhushan Mulmule

[email protected]

Page 2: Understanding Interfaces

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

www.dotnetvideotutorial.com

Page 3: Understanding Interfaces

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

Page 4: Understanding Interfaces

MSDN Defines Interfaces as…

Page 5: Understanding Interfaces

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.

Page 6: Understanding Interfaces

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

with two methods defined in it…

www.dotnetvideotutorial.com

Page 7: Understanding Interfaces

By default every member of interface is public so interface

doesn‘t allow use of access modifier.

Page 8: Understanding Interfaces

Interface doesn't have any implementation.

We have to implement members defined by interface

in class that we will drive from interface.

Page 9: Understanding Interfaces

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

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

Page 10: Understanding Interfaces

Let us implement IDemoInterface in DemoClass

www.dotnetvideotutorial.com

Page 11: Understanding Interfaces

Interface implementation syntax is same as class inheritance.

We need to implement all methods of IDemoInterface in DemoClass

Page 12: Understanding Interfaces

Now we have two ways to create object of DemoClass.

Page 13: Understanding Interfaces

Normal Instantiation: Using reference of DemoClass only

Page 14: Understanding Interfaces

Or

www.dotnetvideotutorial.com

Page 15: Understanding Interfaces

Upcasting: Using reference of IDemoInterface

Page 16: Understanding Interfaces

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

Page 17: Understanding Interfaces

Now let us add little complexity to our example.

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

www.dotnetvideotutorial.com

Page 18: Understanding Interfaces

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

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

Page 19: Understanding 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

Page 20: Understanding Interfaces
Page 21: Understanding Interfaces

Now we have three ways to create object of DemoClass

www.dotnetvideotutorial.com

Page 22: Understanding Interfaces

or

or

Page 23: Understanding Interfaces

First let us have normal instantiation…

Page 24: Understanding Interfaces
Page 25: Understanding Interfaces

Output:

www.dotnetvideotutorial.com

Page 26: Understanding Interfaces

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()

Page 27: Understanding Interfaces

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

ISampleInterface?

www.dotnetvideotutorial.com

Page 28: Understanding Interfaces

We can do it using Explicit Interface Implementation

Page 29: Understanding Interfaces
Page 30: Understanding Interfaces

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

Page 31: Understanding Interfaces

We can’t call explicitly implemented methods using

reference of class.

www.dotnetvideotutorial.com

Page 32: Understanding Interfaces

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.

Page 33: Understanding Interfaces

Million dollar question How to call fun2()?

Page 34: Understanding Interfaces

We can call explicitly implemented methods using

reference of Interface.

Page 35: Understanding Interfaces

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

IDemoInterface

www.dotnetvideotutorial.com

Page 36: Understanding Interfaces

Interface reference has access to all its methods.

Page 37: Understanding Interfaces

Output:

Page 38: Understanding Interfaces

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

Page 39: Understanding Interfaces

www.dotnetvideotutorial.com

Page 40: Understanding Interfaces

Output:

Page 41: Understanding Interfaces

Note that Interface reference has access to its

own methods only. Reference of one interface

can’t call method of other interface.

Page 42: Understanding Interfaces

Is there any way out to call explicitly implemented

method using reference of class?

Page 43: Understanding Interfaces

Yes!We can typecast reference of class to interface

www.dotnetvideotutorial.com

Page 44: Understanding Interfaces

Here we are typecasting reference of DemoClass ; first to IDemoInterface

and then to ISampleInterface.

Page 45: Understanding Interfaces

Output:

Page 46: Understanding Interfaces

We can also derive one interface from other..

www.dotnetvideotutorial.com

Page 47: Understanding Interfaces

Here IDerivedInterface is derived from IBaseInterface

Page 48: Understanding Interfaces

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

Page 49: Understanding Interfaces
Page 50: Understanding Interfaces

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

coupled systems.

www.dotnetvideotutorial.com

Page 51: Understanding Interfaces

For video visit www.dotnetvideotutorial.com