C# Interfaces and RTTI CNS 3260 C#.NET Software Development.

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Object Oriented Programming
Structures Spring 2013Programming and Data Structure1.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics 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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
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.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
CSC241 Object-Oriented Programming (OOP) Lecture No. 19.
Programming Pillars Introduction to Object- Oriented Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object-Oriented Programming: Inheritance and Polymorphism.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Object-Oriented Programming: Polymorphism Chapter 10.
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.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
2.7 Inheritance Types of inheritance
Table of Contents Class Objects.
Yunus Özen 12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen
Packages, Interfaces & Exception Handling
Conditional Statements
Lecture 22 Inheritance Richard Gesick.
Fundaments of Game Design
How to organize and document your classes
Understanding Polymorphism
CIS 199 Final Review.
5. 3 Coding with Denotations
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

C# Interfaces and RTTI CNS 3260 C#.NET Software Development

Interface “A surface forming a common boundary between adjacent regions, bodies, substances, or phases” – Dictionary.com

C# Interfaces Defines object behavior  Methods  Properties  Indexers  Events  Not Variables classes and structs may implement interfaces  Syntax is like inheritance  Multiple interfaces may be implemented  Each Interface member must be implemented if the class implements the interface

Interface Syntax public interface IPlayer { void Play(); } Notice:  interface keyword  No access specifier all members of an interface are inherently public  No function body allowed No implementations allowed  Practice is to start the interface name with an “I”

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 public interface IRadioPlayer : IPlayer { float Volume { get; set; } }

Implementing Interfaces Interface members must be implemented publicly Implementing an interface adds the interface type to the implementing class  MyRadio is-a IRadioPlayer public class MyRadio : IRadioPlayer { private float volume = 50f; public float Volume { get { return volume; } set { volume=value; } } public void Play(int index) { // TODO: Implement Play }

With class Inheritance... Classes may inherit from a class and still implement interfaces  Class inheritance comes first syntactically public class MyEntertainmentCenter : MyRadio, IComparable, IServiceProvider { IComparable Members IServiceProvider Members }

Name Conflicts What if two Interfaces use the same member name??? public interface ICar { void Go(); } public interface IBoat { void Go(); }

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 of either ICar or IBoat will call Go()

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 Interface Demo)

Virtual and override Interface functions are not virtual  It wouldn’t make any sense  You can however make them virtual in the implementing class An override function may satisfy an interface A public function in a base class before the interface will satisfy the interface in a derived class (See Interface Demo)  Normal overriding/hiding rules apply to the derived class

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

Interfaces in the Libraries IComparable  Allows to items to be compared (and thus, sorted) IComparer  Create a custom compare object to override IComparable for standard objects ICloneable  Allows objects to create a deep copy of themselves IDisposable  The class implements a Dispose method ISerializable  Defines how an object may write itself out to disk (serialize itself) IEnumerable  Indicates that the class can be traversed like an array (using foreach) And a whole bunch more