Using Method Over Riding

In this example, the Manager class over rides the getPayCheque() method of the Employee class to include a big fat bonus.


View The Results...

Employee.cfc
<cfcomponent output="false" hint="I am an Abstract class representing an Employee">

	<!--- constructor --->
	<cffunction name="init" 
		access="public" 
		returntype="void"
		output="false"
		hint="I am the constructor">
		<cfset variables.instance = StructNew() />
		<!--- 
		I return void as I am an abstract class 
		and don't want to be called directly
		--->		
	</cffunction>
	
	<!--- public methods --->
	<cffunction name="getPayCheque" 
		access="public" 
		returntype="numeric"
		output="false"
		hint="I return the amount of pay">
		<cfreturn getHourlyRate() * getHoursWorked() />
	</cffunction>
	<cffunction name="getHourlyRate" 
		access="public" 
		returntype="numeric"
		output="false"
		hint="I return the hourly rate">
		<cfreturn variables.instance.hourlyrate />
	</cffunction>
	<cffunction name="getHoursWorked" 
		access="public" 
		returntype="numeric"
		output="false"
		hint="I return the hours worked">
		<cfreturn variables.instance.hoursworked />
	</cffunction>
	
	<cffunction name="setHoursWorked" 
		access="public" 
		returntype="void"
		output="false"
		hint="I set the number of hours worked">
		<cfargument name="hoursworked" 
			type="numeric" 
			required="true"
			hint="I am the number of hours worked" />
		<cfreturn variables.instance.hoursworked />
	</cffunction>
	<cffunction name="setHourlyRate" 
		access="public" 
		returntype="void"
		output="false"
		hint="I set the hourly rate">
		<cfargument name="hourlyrate" 
			type="numeric" 
			required="true"
			hint="I am the rate for each hour worked" />
		<cfreturn variables.instance.hourlyrate />
	</cffunction>
	
</cfcomponent>
Secretary.cfc
<cfcomponent extends="Employee" output="false" hint="I represent a Secretary Employee">

	<!--- constructor --->
	<cffunction name="init" 
		access="public" 
		returntype="Secretary"
		output="false"
		hint="I am the constructor">
		<cfset super.init() />
		<cfreturn this />
	</cffunction>
	
	<!---
	currently our secretary doesn't have any behaviour which is different 
	to a generic employee so just inherit all the methods we need.
	--->
	
</cfcomponent>
Manager.cfc
<cfcomponent extends="Employee" output="false" hint="I represent a Manager Employee">

	<!--- constructor --->
	<cffunction name="init" 
		access="public" 
		returntype="Manager"
		output="false"
		hint="I am the constructor">
		<cfset super.init() />
		<cfreturn this />
	</cffunction>
	
	<!--- public methods --->
	<cffunction name="getPayCheque" 
		access="public" 
		returntype="numeric"
		output="false"
		hint="I return the amount of pay, overriding the Employee.getPayCheque() method">
		<cfreturn ( getHourlyRate() * getHoursWorked() ) + getBonus() />
	</cffunction>
	
	<!--- private methods --->
	<cffunction name="getBonus" 
		access="private" 
		returntype="numeric"
		output="false"
		hint="I return the bonus this manager has earnt">
		<!--- this method would be linked to sales in a real application --->
		<cfreturn 1000 />
	</cffunction>
	
</cfcomponent>

View The Code...

Secretary Pay: 1160
Manager Pay: 3625


« Part 2 Menu