4. Introduction
• Events allow objects to communicate with one
another through a kind of synchronous message
passing.
• Events allow classes to send and receive messages
between one another.
• Events can also be triggered by your applications or
by the operating system.
5. Introduction
• Events enable you to associate function calls with
user actions.
• Events can also be triggered by your applications or
by the operating system.
• In GUI, events are user actions like key press, clicks,
mouse movements, etc., or some occurrence like
system generated notifications.
• Applications need to respond to events when they
occur
6. Introduction
• Objects communicate with one another through
synchronous message passing.
• Events are attached to other functions; objects
register callback functions to an event, and these
callbacks are executed when (and if) the event is
triggered by some object.
7. The Event Class and Event Module
• The Control.Event<'T> Class helps in creating an
observable object or event.
• It has the following instance members to work with
the events −
Member Description
Publish Publishes an observation as a first
class value.
Trigger Triggers an observation using the
given parameters.
8. Creating Events
• Events are created and used through
the Event class.
• The Event constructor is used for creating an event.
• Steps to create event
1. Create Event
2. Expose event handler
3. Invoke Event Handler
4. add callbacks to event handlers.
10. type Worker(name : string, shift : string) =
let mutable _name = name;
let mutable _shift = shift;
//creates event
let nameChanged = new Event<unit>()
let shiftChanged = new Event<unit>()
// exposed event handler
member this.NameChanged = nameChanged.Publish
member this.ShiftChanged = shiftChanged.Publish
11. //invokes event handler
member this.Name
with get() = _name
and set(value) = _name <- value
nameChanged.Trigger()
//invokes event handler
member this.Shift
with get() = _shift
and set(value) = _shift <- value
shiftChanged.Trigger()
12. //add callbacks to event handlers
let w1 = new Worker(“AAAA", "Evening")
w1.NameChanged.Add(fun () -> printfn "Worker changed name! New name:
%s" wk.Name)
wk.Name <- “AMIT“
w1.NameChanged.Add(fun () -> printfn "-- Another handler attached to
NameChanged!")
wk.Name <- “JAIN"
w1.ShiftChanged.Add(fun () -> printfn "Worker changed shift! New shift: %s"
w1.Shift)
w1.Shift <- "Morning“
w1.ShiftChanged.Add(fun () -> printfn "-- Another handler attached to
ShiftChanged!")
wk.Shift <- "Night"
13. Abstract Class
used as a base class of a hierarchy and represents
common functionality
can have implemented and unimplemented
members.
A class that inherits abstract class must provide
implementation of all abstract methods & Class
Abstract classes are used to achieve abstraction.
We cannot create an instance of an abstract class
because the class is not fully implemented.
14. Syntax
[<AbstractClass>]
type [ accessibility-modifier ] abstract-class-name =
[ inherit base-class-or-interface-name ]
[ abstract-member-declarations-and-member-
definitions ]
// Abstract member syntax.
abstract member member-name : type-signature
16. [<AbstractClass>]
type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
type Student(name, studentID : int) =
inherit Person(name)
member x.StudentID = studentID
override x.Greet() = printfn "Student %s" x.Name
type Teacher(name, expertise : string) =
inherit Person(name)
member x.Expertise = expertise
override x.Greet() = printfn "Teacher %s." x.Name
let st = new Student("Amit", 1234)
let tr = new Teacher("Apurva", "Java")
//Overriden Greet
st.Greet()
tr.Greet()
17. [<AbstractClass>]
type AbstractClass1() =
class
abstract member ShowClassName : unit -> unit
end
type DerivedClass1() =
class
inherit AbstractClass1()
override this.ShowClassName() = printf "This is derived class."
end
let a = new DerivedClass1()
a.ShowClassName()
18. Interface
• It provides pure abstraction.
• Abstraction is about hiding unwanted details while showing
most essential information.
• It is a collection of abstract methods.
• A Class which implements interface must provide definition
for its all methods.
20. Abstract Class
In an interface declaration the members are not
implemented.
The members are abstract, declared by
the abstract keyword.
We may provide a default implementation using
the default keyword.
We can implement interfaces either by using object
expressions or by using class types.
In class or object implementation, We need to
provide method bodies for abstract methods of the
interface..
21. Interface :Syntax
[ attributes ]
type interface-name =
[ interface ] [ inherit base-interface-name ...]
abstract member1 : [ argument-types1 -> ] return-type1
abstract member2 : [ argument-types2 -> ] return-type2
...
[ end ]
The keywords interface and end, which mark
the start and end of the definition, are optional
23. Calling Interface Methods
• Interface methods are called through the interface,
• not through the instance of the class or type
implementing interface.
• To call an interface method, you up cast to the
interface type by using the :> operator (upcast
operator).
Example :
(s :> IPerson).Enter()
(s :> IPerson).Leave()
24. Example:1
Person
abstract Name
abstract Enter ()
abstract Leave()
Student
member this.ID
member this.Name
member this.Enter()
member this.Leave()
Teacher
member this.ID
member this.Name
member this.Enter()
member this.Leave()
Implements
25. type Person =
abstract Name : string
abstract Enter : unit -> unit
abstract Leave : unit -> unit
type Student(name : string, id : int) =
member this.ID = id
interface Person with
member this.Name = name
member this.Enter() = printfn "Student entering premises!"
member this.Leave() = printfn "Student leaving premises!"
26. type Teacher(name : string, id : int) =
member this.ID = id
interface Person with
member this.Name = name
member this.Enter() = printfn "Teacher entering premises!“
member this.Leave() = printfn "Teacher leaving premises!“
let s = new Student("Amit", 1234)
let t = new Teacher("Rohit", 34)
(s :> Person).Enter()
(s :> Person).Leave()
(t :> Person).Enter()
(t :> Person).Leave()
27. Interface Inheritance
Interfaces can inherit from one or more base interfaces.
Interface1
abstract member doubleIt
Interface2
abstract member tripleIt
Interface3
inherit Interface1
inherit Interface2
abstract member printIt()
multiplierClass
member this.doubleIt(a)
member this.tripleIt(a)
member this.printIt(a)
Implements
28. type Interface2 =
abstract member tripleIt: int -> int
type Interface3 =
inherit Interface1
inherit Interface2
abstract member printIt: int -> string
type multiplierClass() =
interface Interface3 with
member this.doubleIt(a) = 2 * a
member this.tripleIt(a) = 3 * a
member this.printIt(a) = a.ToString()
let ml = multiplierClass()
printfn "%d" ((ml:>Interface3).doubleIt(5))
printfn "%d" ((ml:>Interface3).tripleIt(5))
printfn "%s" ((ml:>Interface3).printIt(5))
29. //using the subclasses
let p = new Person("Mohan")
let st = new Student("Aditi", 1234)
let tr = new Teacher("Sujit", "Java")
p.Greet()
st.Greet()
st.dispID()
tr.Greet()
tr.disp()
Hi, I'm Mohan
Hi, I'm Aditi
Student ID 1234
Hi, I'm Sujit
Expertise in Java
O/P
33. Differences………
Overloading Overriding
• Overloading occurs when
two or more methods in
one class have the same
method name but
different parameters
• Overriding means having
two methods with the
same method name and
parameters (i.e., method
signature).
• One of the methods is in the
parent class and the other is in
the child class.
34. Introduction
• Operator overloading allows programmers to provide new
behavior for the default operators
• We can redefine operator behavior according to your custom
need.
• Operators are functions with special names, enclosed in
brackets.
• We can overload all arithmetic operators.
• Operators must be defined as static.
• All unary operators must use ~ (tiled) operator to indicate
that this is unary operator.
35. Introduction
• We can redefine or overload most of the built-in operators
available in F#
//overloading + operator
static member (+) (a : Complex, b: Complex) =
Complex(a.x + b.x, a.y + b.y)
The above function implements the addition operator (+) for a user-
defined class Complex.
It adds the attributes of two objects and returns the resultant Complex
object.
37. type Complex(x: float, y : float) =
member this.x = x
member this.y = y
//overloading + operator
static member (+) (a : Complex, b: Complex) =
Complex(a.x + b.x, a.y + b.y)
//overloading - operator
static member (-) (a : Complex, b: Complex) =
Complex(a.x - b.x, a.y - b.y)
// overriding the ToString method
override this.ToString() =
this.x.ToString() + " " + this.y.ToString()
38. //Creating two complex numbers
let c1 = Complex(7.0, 5.0)
let c2 = Complex(4.2, 3.1)
// addition and subtraction using the overloaded operators
let c3 = c1 + c2
let c4 = c1 - c2
//printing the complex numbers
printfn "%s" (c1.ToString())
printfn "%s" (c2.ToString())
printfn "%s" (c3.ToString())
printfn "%s" (c4.ToString())
39. Overriding in F#
• We can override a default behavior of a base class method
and implement it differently in the subclass or the derived
class.
• Methods in F# are not overridable by default.
• To override methods in a derived class, we have to declare a
method as overridable using keyword abstract and default
keyword
type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
default x.Greet() = printfn "Hi, I'm %s" x.Name
40. type Person(name) =
member x.Name = name
abstract Greet : unit -> unit
default x.Greet() = printfn "Hi, I'm %s" x.Name
type Student(name, studentID : int) =
inherit Person(name)
member x.StudentID = studentID
override x.Greet() = printfn "Student %s" x.Name
member x.dispID()=printfn "Student ID %d" x.StudentID
type Teacher(name, expert : string) =
inherit Person(name)
member x.Expert=expert
override x.Greet() = printfn "Teacher %s." x.Name
member x.disp()= printfn "Experties in %s" x.Expert
41. //using the subclasses
let p = new Person("Mohan")
let st = new Student("Aditi", 1234)
let tr = new Teacher("Sujit", "Java")
//default Greet
p.Greet()
//Overriden Greet
st.Greet()
tr.Greet()