Page 1
Page 2
Page 3
Page 4
Page 5

But what the executives really want
to know is how much money is being spent on payroll, company
wide. Rather than having three separate functions to find
out the annual income of our three different objects, we
can use only one function, which will work with any object
that is a descendent of the Employee class (polymorphism).
This means that if tomorrow our boss wanted to add another
class called Executive (not likely right?) to the cost cutting
program, we would not need to change much of our existing
code. All functions that take Employees, would be
able to accept our new Executive objects with no problem.
To determine the total amount of money
spent on payroll, I created a function called AnnualIncome
that takes an Employee object (or any objects that inherited
from Employee) and returns its AnnualIncome property.
Public Function AnnualIncome(obj As Employee)
As Double
AnnualIncome = Employee.GetAnnualIncome(obj)
End Function
Notice that to achieve polymorphism,
I am calling methods of the base (Employee) class.
The reason this works is because the Employee class file
keeps track of FirstName, LastName and AnnualIncome, not
only for all Employees, but also for all objects that inherit
from Employee. Remember that when we call Intern.setFirstName
we actually pass on the information to the Employee
class. Our Intern class simply provides a wrapper around
the Employee class for the methods and properties that Employee
has defined, which are inherited by our Intern class. Because
we are querying a property that was defined in the base
class, we know that all objects that inherited from Employee
have an AnnualIncome property being stored in the
Employee class file, making it legal to pass in an Intern
object into Employee.GetAnnualIncome().
The result is an effective list of all
employees, managers, and interns along with their salary,
and the total payroll amount.

People who know and work with OO will
be the first to tell you that a true OO language allows
for much better implementation than what I did here. But
we were able to achieve many of OOs benefits in this
simple program without the help of an OO language!
- We were able to encapsulate all the
properties and methods of a class into a form file in
lieu of a class file, allowing us to make private functions
private, and public functions public.
- We were able to achieve inheritance
by wrapping the base class methods and passing on the
information from inheriting classes.
- We were able to create a function
that could work with any descendent of Employee to demonstrate
polymorphism.
I could reuse my Employee class in any
project, and if I wanted to replace any part of my program,
it would not be difficult because of the compartmentalization
of OOP. Please check out the source code for a more indepth
look at how I simulated OOP in Embedded Visual Basic. I
welcome any questions and feedback, because thats
what makes this site great.
Previous Page