c# interfaces c# class version 1.0. copyright © 2012 by dennis a. fairclough all rights reserved. 2...

20
C# Interfaces C# Class Version 1.0

Upload: angelica-armstrong

Post on 14-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

C# Interfaces

C# Class

Version 1.0

Page 2: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2

Interface

“A surface forming a common boundary between adjacent regions, bodies,…” – Dictionary.com

Page 3: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

C# Standard

An interface type defines a contract as a named set of public method prototypes. A class or struct that implements an interface must provide implementations of the interface’s method prototypes. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 3

Page 4: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 4

Interfaces in Programming The member(s) that a class or an object expose(s) is

its interface Classes can implicitly or explicitly share an interface

Classes which share common member(s) but are not related implicitly share an interface STL containers

Classes with a common type explicitly share an interface An explicit interface defines a contract

C# has such an interface construct!

Page 5: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Interface (Contract) Like a class except for:

Provides only a specification (prototype) for one or more member methods, properties, indexers, events. implicity abstract and public, implied &

required, i.e. virtual and non-static. Classes and struct’s can implement

(inherit) one or more interfaces “…we defined polymorphism as the ability to perform the same

operations on many types, as long as each type shares a common subset of characteristics. The purpose of an interface is to precisely define such a set of characteristics.”

(C# Essentials 2nd Edition)

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 5

Page 6: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 6

C# Interfaces Define object signatures, names and return data types for:

Methods Properties Indexers Events Not Variables!

classes and structs may implement zero or more interfaces Accomplished through no inheritance, single, multiple

inheritance A class or struct may implement zero or more interfaces Each Interface member must be implemented in the class

that implements that interface

Page 7: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

C# Standard Interfaces Overview

An interface has the following characteristics: Similar to an abstract base class: any non-abstract type inheriting

the interface must implement all its members. Adapters are used to modify this constraint. Cannot be instantiated directly. Can contain events, indexers, methods and properties. Contain no implementation of methods. Classes and structs can inherit from zero or more interfaces. An interface can itself inherit from zero or more interfaces

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 7

Page 8: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 8

Defining an Interfacepublic interface IPlayer{ void Play(); //method prototype}

Notice: interface keyword Only method prototypes are allowed No access specifier allowed on members

all members of an interface are inherently public No function bodies allowed

An interface has no method implementations Common practice is to start the interface name

with an “I” i.e. IPlayer

Page 9: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 9

Interface Inheritance Interfaces may inherit from other interfaces

Any class implementing the interface must implement all the methods of all the interfaces in the inheritance chain

Multiple interface inheritance and hierarchies are allowed

public interface IRadioPlayer : IPlayer{

double Volume //property prototype{

get;set;

}}

Page 10: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 10

Implementing Interfaces Interface members always

implemented publicly Implementing an interface

adds the interface type to the implementing class MyRadio is-a

IRadioPlayer MyRadio is-a IPlayer

public class MyRadio : IRadioPlayer{ private double volume = 50f;

public double Volume { get { return volume; } set { volume=value; } }

public void Play() { // TODO: Implement Play }}

Page 11: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 11

With class Inheritance...

Classes may inherit from a class and still implement interfaces Class inheritance MUST come first

followed by the interface(s)

public class MyEntertainmentCenter : MyRadio, IComparable, IServiceProvider{

IComparable Members

IServiceProvider Members}

Page 12: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 12

Name Conflicts

What if two Interfaces use the same member name? Not good practice!

public interface ICar{ void Go();}

public interface IBoat{ void Go();}

Page 13: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 13

The Infamous Car-Boatpublic class TheInfamousCarBoat : ICar, IBoat{ public void Go() { Console.WriteLine("Going... (Wet or dry? I don't know.)"); }}

The Implementation of Go() satisfies both interfaces Reference for either ICar or IBoat will call Go()

Page 14: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 14

The Amphibious Vehicle

Implements each explicitly private to the class public to the appropriate Interface An interface reference will call the appropriate method

public class AmphibiousVehicle : ICar, IBoat{ void IBoat.Go() { Console.WriteLine("IBoat.Go()... Floating"); }

void ICar.Go() { Console.WriteLine("ICar.Go()... Driving"); }}

(See Interfaces Demo)

Page 15: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 15

virtual and override Interface methods are not virtual

It wouldn’t make any sense You can however make them virtual in the implementing class

A override or new modified method may satisfy the interface

A public function in a base class before the interface will satisfy the interface in a derived class

Normal overriding/new hiding rules apply to the derived class

Page 16: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 16

The Proper use of Interfaces Allow you to separate object definition from

implementation Once an interface is set, changing it can be a big deal

Especially if there is code programmed to the interface already

Adapters can be used to solve this problem

InterfaceIBetween

Programmer Acreates

Module AimplementsIBetween

Programmer Bcreates

Module Buses an

IBetween

Page 17: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Interface ValueType or Reference Type Variables

ValueType or Reference Type Variables my be cast to an interface type.

IEnumerable inumb = new MyObject(); What is the implication?

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 17

Page 18: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 18

Interfaces in the C# Libraries

IComparable Provides for items to be compared (and thus, sorted)

IComparer Provides for objects to be compared and to override IComparable for

standard objects ICloneable

Provides for objects to create a copy of themselves IDisposable

Forces the class to implement the Dispose method ISerializable

Provides for an object to write itself out to disk (serialize itself) IEnumerable

Provides for a class to be traversed like an array (using foreach) And a whole bunch more!

Page 19: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

IEnumerable & IEnumerator

See Example Code & Lab

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 19

Page 20: C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 20

What did you learn?

??