The Super keyword

The super keyword allows you to access a method in the superclass that you have over ridden.


View The Results...

Product.cfc
<cfcomponent output="false" hint="I represent a product">

	<!--- constructor --->
	<cffunction name="init" 
		access="public" 
		returntype="void"
		output="false"
		hint="I am the constructor">
		<cfargument name="populate"
			type="struct"
			required="false"
			default="#StructNew()#"
			hint="I am the values for the product properties" />
			
		<cfif StructKeyExists( arguments.populate, "sku" )>
			<cfset setSku( arguments.populate.sku ) />
		</cfif>
		<cfif StructKeyExists( arguments.populate, "price" )>
			<cfset setPrice( arguments.populate.price ) />
		</cfif>
		<cfif StructKeyExists( arguments.populate, "title" )>
			<cfset setTitle( arguments.populate.title ) />
		</cfif>
		<cfif StructKeyExists( arguments.populate, "description" )>
			<cfset setDescription( arguments.populate.description ) />
		</cfif>
		<!--- 
		I return void as I am an abstract class 
		and don't want to be called directly
		--->
	</cffunction>
	
	<!--- public methods --->
	<cffunction name="getSku" 
		access="public" 
		returntype="string"
		output="false">
		<cfreturn variables.instance.sku />
	</cffunction>
	<cffunction name="getPrice" 
		access="public" 
		returntype="numeric"
		output="false">
		<cfreturn variables.instance.price />
	</cffunction>
	<cffunction name="getTitle" 
		access="public" 
		returntype="string"
		output="false">
		<cfreturn variables.instance.title />
	</cffunction>
	<cffunction name="getDescription" 
		access="public" 
		returntype="string"
		output="false">
		<cfreturn variables.instance.description />
	</cffunction>
	
	<!--- private methods --->
	<cffunction name="setSku" 
		access="private" 
		returntype="void"
		hint="I set the sku"
		output="false">
		<cfargument name="value" 
			required="true"
			type="string" />
		<cfset variables.instance.sku = arguments.value />
	</cffunction>
	<cffunction name="setPrice" 
		access="public" 
		returntype="void"
		hint="I set the price"
		output="false">
		<cfargument name="value" 
			required="true"
			type="numeric" />
		<cfset variables.instance.price = arguments.value />
	</cffunction>
	<cffunction name="setTitle" 
		access="private" 
		returntype="void"
		hint="I set the title"
		output="false">
		<cfargument name="value" 
			required="true"
			type="string" />
		<cfset variables.instance.title = arguments.value />
	</cffunction>
	<cffunction name="setDescription" 
		access="private" 
		returntype="void"
		hint="I set the description"
		output="false">
		<cfargument name="value" 
			required="true"
			type="string" />
		<cfset variables.instance.description = arguments.value />
	</cffunction>
</cfcomponent>
DVDProduct.cfc
<cfcomponent extends="Product" output="false" hint="I represent a DVD product">

	<!--- constructor --->
	<cffunction name="init" 
		access="public" 
		returntype="Product"
		output="false"
		hint="I am the constructor">
		<cfargument name="populate"
			type="struct"
			required="false"
			default="#StructNew()#"
			hint="I am the values for the product properties" />

		<!--- call the parent/super class init method --->
		<cfset super.init( arguments.populate ) />
			
		<cfif StructKeyExists( arguments.populate, "region" )>
			<cfset setRegion( arguments.populate.region ) />
		</cfif>
		<cfif StructKeyExists( arguments.populate, "duration" )>
			<cfset setDuration( arguments.populate.duration ) />
		</cfif>

		<cfreturn this />
	</cffunction>
	
	<!--- public methods --->
	<cffunction name="getRegion" 
		access="public" 
		returntype="numeric"
		output="false">
		<cfreturn variables.instance.region />
	</cffunction>
	<cffunction name="getDuration" 
		access="public" 
		returntype="numeric"
		output="false">
		<cfreturn variables.instance.duration />
	</cffunction>
	
	<cffunction name="setRegion" 
		access="public" 
		returntype="void"
		hint="I set the region"
		output="false">
		<cfargument name="value" 
			required="true"
			type="numeric" />
		<cfset variables.instance.region = arguments.value />
	</cffunction>
	<cffunction name="setDuration" 
		access="public" 
		returntype="void"
		hint="I set the duration"
		output="false">
		<cfargument name="value" 
			required="true"
			type="numeric" />
		<cfset variables.instance.duration = arguments.value />
	</cffunction>
	
</cfcomponent>
BookProduct.cfc
<cfcomponent extends="Product" output="false" hint="I represent a Book product">

	<!--- constructor --->
	<cffunction name="init" 
		access="public" 
		returntype="Product"
		output="false"
		hint="I am the constructor">
		<cfargument name="populate"
			type="struct"
			required="false"
			default="#StructNew()#"
			hint="I am the values for the product properties" />

		<!--- call the parent/super class init method --->
		<cfset super.init( arguments.populate ) />
		
		<cfif StructKeyExists( arguments.populate, "isbn" )>
			<cfset setIsbn( arguments.populate.isbn ) />
		</cfif>
		<cfif StructKeyExists( arguments.populate, "pages" )>
			<cfset setPages( arguments.populate.pages ) />
		</cfif>
		
		<cfreturn this />
	</cffunction>
	
	<!--- public methods --->
	
	<cffunction name="getSku" 
		access="public" 
		returntype="string"
		output="false"
		hint="I override the getSku() method of the superclass">
		<cfreturn getIsbn() />
	</cffunction>
	
	<cffunction name="getIsbn" 
		access="public" 
		returntype="string"
		output="false">
		<cfreturn variables.instance.isbn />
	</cffunction>
	<cffunction name="getPages" 
		access="public" 
		returntype="numeric"
		output="false">
		<cfreturn variables.instance.pages />
	</cffunction>
	
	<cffunction name="setIsbn" 
		access="public" 
		returntype="void"
		hint="I set the isbn"
		output="false">
		<cfargument name="value" 
			required="true"
			type="string" />
		<cfset variables.instance.isbn = arguments.value />
	</cffunction>
	<cffunction name="setPages" 
		access="public" 
		returntype="void"
		hint="I set the pages"
		output="false">
		<cfargument name="value" 
			required="true"
			type="string" />
		<cfset variables.instance.pages = arguments.value />
	</cffunction>
	
</cfcomponent>
index.cfm
<!--- create a DVD --->	
<cfset properties = StructNew() />
<cfset properties.sku = 12345 />
<cfset properties.price = 9.99 />
<cfset properties.title = "The Shawshank Redemption" />
<cfset properties.description = "Andy finds that survival comes down to a simple choice: get busy living or get busy dying." />
<cfset properties.region = 2 /> 
<cfset properties.duration = 2.16 />	
	
<cfset DVD = CreateObject( "component", "DVDProduct" ).init( properties ) />
	
<!--- create a Book --->	
<cfset properties = StructNew() />
<cfset properties.sku = 67890 /> 
<cfset properties.price = 30.99 />
<cfset properties.title = "ColdFusion 8 Developer Tutorial" />
<cfset properties.description = "This book is the most intense guide to creating professional ColdFusion applications available." />
<cfset properties.isbn = "978-1847194121" /> 
<cfset properties.pages = 400 />

<cfset Book = CreateObject( "component", "BookProduct" ).init( properties ) />

<!--- show DVD --->
Product: #DVD.getTitle()# (Region #DVD.getRegion()#)<br />
&pound;#NumberFormat( DVD.getPrice(), "9.99" )#<br />
#DVD.getDescription()#<br />

Playing Time: #DVD.getDuration()#
Stock Code: #DVD.getSku()#
<hr />

<!--- show Book --->
Product: #Book.getTitle()# (#Book.getPages()# Pages)<br />
&pound;#NumberFormat( Book.getPrice(), "9.99" )#<br />
#Book.getDescription()#<br />
ISBN: #Book.getSku()#
</cfoutput>

View The Code...

Product: The Shawshank Redemption (Region 2)
£9.99
Andy finds that survival comes down to a simple choice: get busy living or get busy dying.
Playing Time: 2.16
Stock Code: 12345
Product: ColdFusion 8 Developer Tutorial (400 Pages)
£30.99
This book is the most intense guide to creating professional ColdFusion applications available.
ISBN: 978-1847194121

« Part 2 Menu