Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann

Slides:



Advertisements
Similar presentations
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Advertisements

Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Interfaces and polymorphism Chapter 9. Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum.
Interfaces and Polymorphism Lesson - 8. Objectives Interfaces Supertype and subtype references Polymorphism Inner classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
What is an Interface?? An interface is a ‘container’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Interfaces and Polymorphism.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Object Oriented Programming Inheritance and Polymorphism Dr. Mike Spann
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Chapter 9  Interfaces and Polymorphism 1 Chapter 9 Interfaces and Polymorphism.
C++ fundamentals.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Interfaces and Polymorphism 9.1 Using Interfaces for Code Reuse Defining an Interface implementing an interface 9.2 Converting between Class.
What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
EE2E1. JAVA Programming Lecture 3 Inheritance. Contents Base classes and derived classes Base classes and derived classes Protected scope Protected scope.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
Object Oriented Programming.  Interface  Event Handling.
1 CSC241: Object Oriented Programming Lecture No 25.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Fall 2006Adapted from Java Concepts Companion Slides1 Interfaces and Polymorphism Advanced Programming ICOM 4015 Lecture 10 Reading: Java Concepts Chapter.
CHAPTER 9 INTERFACES AND POLYMORPHISM Chapter Goals: –To learn about interfaces –To be able to convert between supertype and subtype references –To understand.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CHAPTER 9 INTERFACES AND POLYMORPHISM. CHAPTER GOALS To learn about interfaces To be able to convert between supertype and subtype references To understand.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
EE2E1. JAVA Programming Lecture 4 Interfaces. Contents Interfaces – introduction Interfaces – introduction Example – generic sorting Example – generic.
Modern Programming Tools And Techniques-I
Chapter Goals To be able to declare and use interface types
Delegates and Events 14: Delegates and Events
Chapter Eleven Handling Events.
Interfaces and Polymorphism
Chapter 11 – Interfaces and Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Lecture 22 Inheritance Richard Gesick.
Chapter 11 Interfaces and Polymorphism
Object Oriented Programming
Delegates & Events 1.
Inheritance Inheritance is a fundamental Object Oriented concept
Object Oriented Programming
Presentation transcript:

Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann

Contents Introduction to interfaces Introduction to interfaces Example – An IMeasureable interface Example – An IMeasureable interface Callback functions Callback functions Delegates Delegates Events and event handlers Events and event handlers Summary Summary

Introduction to interfaces We have already seen how an abstract base class links related derived class through inheritance We have already seen how an abstract base class links related derived class through inheritance  The overridden virtual methods of the abstract base class must be implemented in the derived classes There are also occasions when we want unrelated classes to exhibit similar behaviour There are also occasions when we want unrelated classes to exhibit similar behaviour  For example we may want to be able to implement a sorting algorithm on unrelated classes

Introduction to interfaces For example, we may want to sort objects of class Square on the basis of the length of their side We could easily do this through polymorphism by implementing an abstract base class Sortable and using similar generic programming techniques we looked at in the last lecture We could easily do this through polymorphism by implementing an abstract base class Sortable and using similar generic programming techniques we looked at in the last lecture  But, C# (along with Java) doesn’t support multiple inheritance

Introduction to interfaces Shape Square Sortable

Introduction to interfaces An interface is simply a list of public methods that any class which implements that interface must provide implementations of Unrelated classes can implement a single interface   In our example, we can define a ISortable interface such that objects of any class which implements this interface can be sorted on the basis of some comparative measure   Our Square class can implement the ISortable interface using the length of side as the comparative measure

Introduction to interfaces public interface ISortable { int compareTo(ISortable s); } Our ISortable interface contains a single public method compareTo() which defines how the comparison is made  This method is overridden in classes which implement this interface

Introduction to interfaces compareTo(ISortable b) returns –1,0 or 1 depending on whether some chosen instance field f of a ISortable object is such that :   this.f<b.f   this.f==b.f   this.f>b.f We can implement this method in class Square to sort on the basis of length of side

Introduction to interfaces public class Square : Shape, ISortable { private int side; public Square(int s) { side = s; } public override void draw() { } public override double area() { return side * side; } public int compareTo(ISortable s) { Square sq=(Square)s; if (side<sq.side) return -(1); if (side > sq.side) return 1; return 0; }

Introduction to interfaces We can provide a ShellSort() method which implements the shell sort algorithm on arrays on ISortable objects   The compareTo() method of the objects implementing the interface is called inside ShellSort()

Introduction to interfaces public class ArrayAlg { public static void shellSort(ISortable[] a) { int n = a.Length; int incr = n / 2; while (incr >= 1) { for (int i = incr; i < n; i++) { ISortable temp = a[i]; int j = i; while (j >= incr && temp.compareTo(a[j - incr]) < 0) { a[j] = a[j - incr]; j -= incr; } a[j] = temp; } incr /= 2; }

Introduction to interfaces To test our program we simply create an array of Square objects and pass it into the ShellSort() method   Because of the cast in the compareTo() method in Square, the correctly implemented compareTo() method is called through polymorphism

Introduction to interfaces public class SortablesTest { static void Main(string[] args) { Square[] s = new Square[3]; s[0] = new Square(3); s[0] = new Square(2); s[0] = new Square(1); ArrayAlg.shellSort(s);// Sorts on length of side }

An IMeasureable interface Suppose we have a DataSet class which computes simple statistics of numbers read from an input stream   For example, the average and maximum DataSet average maximum Input stream

public class DataSet { public DataSet() { sum = 0.0; maximum = 0.0; count = 0; } public void add(double x) { sum += x; if (count == 0 || maximum < x) maximum = x; count++; } public double getAverage() { if (count == 0) return 0; else return sum / count; } public double getMaximum() { return maximum; } private double sum, maximum; private int count; }

An IMeasureable interface Clearly we would have to modify the DataSet class if we wanted to get the average of a set of bank account balances or to find the coin with the highest value amongst a set   DataSet is not re-useable as it stands   However, if all classes that DataSet objects operate on implement an IMeasurable interface, then the class becomes more flexible

An IMeasureable interface Thus getMeasure() for BankAccount objects return the balance and for Coin objects returns the coin value public interface IMeasureable { double getMeasure(); }

An IMeasureable interface public class BankAccount: IMeasureable { private double balance; private int accountNumber;. double getMeasure() { return balance;} } public class Coin: IMeasurable { private double value;. double getMeasure() { return value;} }

An IMeasureable interface The IMeasurable interface expresses the commonality amongst objects   The fact that each measurable objects can return a value relating to its size DataSet objects can then be used to analyse collections of objects of any class implementing this interface with minor modifications to the code

public class DataSet { public DataSet() { sum = 0.0; count = 0; } public void add(IMeasureable x) { sum += x.getMeasure(); if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; count++; } public double getAverage() { if (count == 0) return 0; else return sum / count; } public double getMaximum() { return maximum.getMeasure(); } private IMeasureable maximum; private double sum; private int count; }

An IMeasureable interface class MeasureablesTest { static void Main(string[] args) { DataSet d=new DataSet(); Coin c1=new Coin(10); Coin c2=new Coin(20); d.add(c1); d.add(c2); double maxCoin=d.getMaximum(); System.Console.WriteLine("coin max= " + maxCoin); }

Callback functions The DataSet class is useful as a re-usable class but is still limited   The IMeasurable interface can only be implemented by user defined classes   We can’t, for example, find the maximum of a set of Rectangle objects as Rectangle is a pre- defined class   We can only measure an object in one way. For example, in the case of BankAccount objects, we can only measure it in terms of the balance

Callback functions The solution is to delegate the measuring to a separate class rather than being the responsibility of the objects we are measuring   We can create a separate IMeasurer interface and implement a measure() method in objects implementing this interface public interface IMeasurer { double measure(Object anObject); }

public class DataSet { public DataSet(IMeasurer m) {measurer=m;} public void add(Object x) {sum=sum+measurer.measure(x); if (count==0 || maximum<measurer.measure(x)) maximum=measurer.measure(x);count++;} public double getMaximum() { return maximum; return maximum; } private IMeasurer measurer; private double sum; private double sum; private int count; private int count; private double maximum; private double maximum;} Callback functions

A DataSet object makes a callback to the measure() method of an object implementing the IMeasurer interface when it needs to measure an object (such as checking a bank balance)   This is in contrast to calling the getMeasure() method of an object implementing the IMeasurable interface We are now free to design any kind of measures on an object of any class   For example, we can measure Square objects by area   We require a SquareMeasurer class which implements the IMeasurer interface

Callback functions public class Square : Shape { private int side; public Square(int s) { side = s; } public override void draw() { } public override double area() { return side * side; } } public class SquareMeasurer : IMeasurer { public double measure(Object anObject) { Square sq=(Square) anObject; return sq.area(); }

Callback functions class MeasurersTest { static void Main(string[] args) { IMeasurer m = new SquareMeasurer(); DataSet data = new DataSet(m); // Add squares to the data set data.add(new Square(5));// Callback to m.measure() data.add(new Square (30));// Callback to m.measure() // Get maximum double max = data.getMaximum(); }

Callback functions Adding square objects to data enforces callbacks to the measure() method of the SquareMeasurer object to be made We have flexibility over our implementation of SquareMeasurer so that any feature of Square objects can be measured   Or even defining several measurer classes to measure different features Callbacks are used extensively in building graphical user interfaces   A callback function is added to an event object which is called when the event is triggered   A managed way of doing this is to encapsulate the callback function into a delegate object

Delegates A delegate object holds a reference to a method with a pre-defined signature   A signature is simply the argument list and return type of the method The keyword delegate specifies that we are defining a delegate object   For example we can define a delegate object myDelegate which holds a method which returns void and takes an int and a double as arguments public delegate void myDelegate(int arg1, double arg2)

Delegates A delegate object is initialized with a (callback) method   The method signature must match the delegate signature public delegate void myDelegate(int arg1, double arg2); public class App { public static void Main() { myDelegate call = new myDelegate(aMethod); // We can now treat call as a simple object and pass // it around as such } static void aMethod(int k, double x) {} }

Delegates Delegate objects can be initialized with several method calls using the += operator   The method calls can then be invoked in a chain by passing the correct arguments to the delegate object   Essentially it amounts to calling methods through a proxy object and is a powerful mechanism for event handling as we shall see   Passing method calls into objects is then equivalent to passing delegate objects

Delegates myDelegate(int,double) call aMethod( ) anotherMethod( ) Invoked by call(1,2.0) aMethod(1,2.0) anotherMethod(1,2.0)

Delegates public delegate void myDelegate(int arg1, double arg2); public class App { public static void Main() { myDelegate call = new myDelegate(aMethod); call += new myDelegate(anotherMethod); call(1, 2.0); } static void aMethod(int k, double x) { System.Console.WriteLine("aMethod " + k + " " + x); } static void anotherMethod(int k, double x) { System.Console.WriteLine("anotherMethod " + k + " " + x); }

Delegates

Events and event handlers C# has built in support for event handling Event handling is usually used in GUI’s such as to notify an application when a mouse click or button press has occurred But any type (not just graphical types) can use events to allow notification of any kind A type must register its interest in handling a specific event with an event handler with a pre-defined signature   This is achieved using a delegate object

Events and event handlers A type can define an event and a corresponding event handler which is a delegate object public class MyType { public delegate void EventHandler(int arg1, int arg2); public event EventHandler myEvent;// defines myEvent // and corresponding // event handler. }

Events and event handlers An application can then register its interest in an event by adding an initialized delegate object to the event using the += operator public class App { public static void Main() { MyType m = new MyType(); m.myEvent += new MyType.EventHandler(myHandler); } public static void myHandler(int i1, int i2) { // Event handler code }

Events and event handlers Equivalent code is very common in GUI development where applications register their own event handlers to respond to events generated by graphical components such as button clicks   Normally such code is automatically generated if we are using visual programming techniques (‘drag and drop’)   However, it is still important to understand how it all works

Events and event handlers For example the following code snippet registers a graphical applications event handler to respond to button clicks public class MyGraphicalApp { Button button = new Button(); button.Click += new EventHandler(HandleButtonClick); public void HandleButtonClick(Object sender EventArgs e) { // Event handler code } public class Button { public delegate void EventHandler(.....); public event EventHandler Click;. }

Events and event handlers For example, we can generate our own SafeArray class which fires an event on trying to access it beyond its bounds We pass the array index into the event handler   In our simple application, the event handler simply prints out an error message   In an embedded system application, the array could be re-allocated to be a larger size in the event handler

Events and event handlers public class SafeArray { public delegate void OutOfBoundsEventHandler(int arg1); public event OutOfBoundsEventHandler myOutOfBoundsEvent; private int[] data; private int numberOfElements=0; public SafeArray(int n) { numberOfElements = n; data = new int[numberOfElements]; } public int access(int elem) { if (elem < numberOfElements) return data[elem]; else myOutOfBoundsEvent(elem); // Fire an event return 0; }

Events and event handlers public class App { public static void Main() { SafeArray s = new SafeArray(10); s.myOutOfBoundsEvent += new SafeArray.OutOfBoundsEventHandler(myHandler); s.access(7); s.access(11);// Out of bounds event generated! } public static void myHandler(int i1) { System.Console.WriteLine("Index " + i1 + " out of bounds "); }

Summary We have seen how objects of unrelated classes can exhibit similar behaviour by implementing an interface Interfaces support generic programming and avoid multiple inheritance We looked at a detailed example involving a IMeasurable interface   We also introduced callbacks by having a separate measurer object implementing an IMeasurer interface   A callback function was made to a measure() method which returned some measure on the object passed to it We have seen how delegates are objects which encapsulate method calls   These method calls can be chained using the += operator We have seen how we can create types which can generate events and how applications can register their interest in events by initializing a delegate object with their own event handler