Home

Class Inheritance: Object Casting - Up-Casting

   

Description

We are already familiar with conversion functions that are used to convert a value from one type to another. Here is an example that calls the int() function to convert a floating point number to an integer:

let a = 628.305
let b = int a

The most important issue about conversion is that the types must be compatible. The conversion primarily follows the logic by which the number of bits used by, or to hold the values of, a type A is compatible with the number of bits used by, or to hold the values of, a type B. In this case, the value of type A can be converted to a type B. As a result, if the values are not compatible, the conversion will fail.

Just as conversion is possible among values of primitive types, conversion is also possible among objects created from classes. Just as primitive conversion must follow compatible types, object conversion must follow compatibility. When it comes to objects, conversion is based on class hierarchy, which means that inheritance must be established between two classes. Therefore, when it comes to classes, conversion is referred to as casting. Two options exist: downcasting and upcasting.

Up-Casting an Object

Based on a relationship between a child class and its parent, an object created from a child class can be converted to the parent class. This is referred to as up-casting or upcasting. The casting is carried by the :> operator. The formula to follow is:

Derived-Object :> Parent-Class

In this formula, the Derived-Object can be a variable declared from the a class that is derived from another class. The Parent-Class is a class aligned in the upper ancestry of the class from which the variable was declared. Here is an example:

type Person(first, last) =
    member this.FirstName : string = first
    member this.LastName  : string = last

type Customer(first, last, acntNumber) =
    inherit Person(first, last)
        member this.AccountNumber : string = acntNumber
        member this.FullName = base.LastName + ", " + base.FirstName

let client = new Customer("Frank", "Justice", "6148-795-8350")

let individual = client :> Person

In the same way, an object created from anywhere down a hierarchy can be cast to a class up the hierarchy. Here is an example:

type Person(first, last) =
    member this.FirstName : string = first
    member this.LastName  : string = last

type Customer(first, last, acntNumber)=
    inherit Person(first, last)
        member this.AccountNumber : string = acntNumber
        member this.FullName = base.LastName + ", " + base.FirstName

type Employee(emplNumber, first, last, acntNumber, employmentStatus, salary) =
    inherit Customer(first, last, acntNumber)
        member this.EmployeeNumber : string = emplNumber
        member this.EmploymentStatus : string = "Full-Time"
        member this.Salary : int = salary

let staffMember = new Employee("9792-4085", "Jonathan", "Lawson", "3741-957-9407", "Part-Time", 48000)

let persFirstName = (staffMember :> Person).FirstName
let persLastName  = (staffMember :> Person).LastName

let clientFirstName  = (staffMember :> Customer).FirstName
let clientLastName   = (staffMember :> Customer).LastName
let clientAcntNumber = (staffMember :> Customer).AccountNumber

When upcasting, if you are using Microsoft Visual Studio, the Intellisense will show the members of the upper-class:

Up-Casting an Object

Up-Casting an Object

     
     
 

Home Copyright © 2009-2015, FunctionX Home