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.

Slides:



Advertisements
Similar presentations
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Multiple Choice Solutions True/False a c b e d   T F.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Polymorphism & Interfaces
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Software Development
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
C# Interfaces and RTTI CNS 3260 C#.NET Software Development.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Yunus Özen 12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen
Interfaces.
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSE 1030: Implementing GUI Mark Shtern.
Fundaments of Game Design
Understanding Polymorphism
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

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 adjacent regions, bodies,…” – Dictionary.com

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

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!

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 2 nd Edition) Copyright © 2012 by Dennis A. Fairclough all rights reserved. 5

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

C# Standard  Interfaces Overview An interface has the following characteristics: Interfaces Overview 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

8 Defining an Interface public 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

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; } }

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 }

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 }

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

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 13 The Infamous Car-Boat public 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()

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)

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

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 Interface IBetween Programmer A creates Module A implements IBetween Programmer B creates Module B uses an IBetween

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

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!

IEnumerable & IEnumerator  See Example Code & Lab Copyright © 2012 by Dennis A. Fairclough all rights reserved. 19

Copyright © 2012 by Dennis A. Fairclough all rights reserved. 20 What did you learn?  ??