2. Base Class & Sub class (Inheritance)
• Inheritance is most important concepts in OOP.
• Inheritance allows us to define a sub class which will inherit
the features of Base class.
• This existing class is called the base class, and the new class is
referred to as the derived/Sub class.
• Reusability and fast implementation.
• The idea of inheritance implements the IS-A relationship.
3. Base Class & Sub class
• A subclass inherits the members of the base class, as well as
has its own members.
• A subclass is defined using the inherit keyword as shown
below .
• In F#, a class can have at most one direct base class. If you do
not specify a base class by using the inherit keyword, the class
implicitly inherits from Object.
type MyDerived(...) =
inherit MyBase(...)
4. Base Class & Sub class
Important Point to Remember
1. The methods and members of the base class are available to
users of the derived class like the direct members of the
derived class.
2. Let bindings and constructor parameters are private to a class
and, therefore, cannot be accessed from derived classes.
3. The keyword base refers to the base class instance. It is used
like the self-identifier.
5. Example
1. Create Person Base
Class
2. Create Student Sub
Class from Base class
Person
3. Create Teacher Sub
Class from Base Class
Person
Person
Name
Greet()
Student
StudentID
dispID()
Teacher
Expert
disp()
6. type Person(name) =
member x.Name = name
member x.Greet() = printfn "Hi, I'm %s" x.Name
type Student(name, studentID : int) =
inherit Person(name)
member x.StudentID = studentID
member x.dispID()=printfn "Student ID %d" x.StudentID
type Teacher(name, expert : string) =
inherit Person(name)
member x.Expert=expert
member x.disp()= printfn "Experties in %s" x.Expert
7. //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
11. 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.
12. 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.
13. 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.
15. 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()
16. //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())
17. 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
18. 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
19. //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()