Using Method Over Riding
In this example, we have over ridden the getSku() method in the DVDProduct class to return the ISBN.
<cfcomponent output="false" hint="I represent a product"> <!--- 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="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> <cffunction name="setSku" access="public" 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="public" 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="public" returntype="void" hint="I set the description" output="false"> <cfargument name="value" required="true" type="string" /> <cfset variables.instance.description = arguments.value /> </cffunction> </cfcomponent>
<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"> <cfset super.init() /> <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>
<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="sku" type="string" required="false" hint="I am the sku" /> <cfargument name="price" type="numeric" required="false" hint="I am the price" /> <cfargument name="title" type="numeric" required="false" hint="I am the title" /> <cfargument name="description" type="numeric" required="false" hint="I am the description" /> <cfargument name="region" type="numeric" required="false" hint="I am the region" /> <cfargument name="duration" type="numeric" required="false" hint="I am the duration" /> <cfset super.init() /> <cfif StructKeyExists( arguments, "sku" )> <cfset setSku( arguments.sku ) /> </cfif> <cfif StructKeyExists( arguments, "price" )> <cfset setPrice( arguments.price ) /> </cfif> <cfif StructKeyExists( arguments, "title" )> <cfset setTitle( arguments.title ) /> </cfif> <cfif StructKeyExists( arguments, "description" )> <cfset setDescription( arguments.description ) /> </cfif> <cfif StructKeyExists( arguments, "region" )> <cfset setRegion( arguments.region ) /> </cfif> <cfif StructKeyExists( arguments, "duration" )> <cfset setDuration( arguments.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>
<!--- create a DVD ---> <cfset DVD = CreateObject( "component", "DVDProduct" ).init() /> <cfset DVD.setSku( 12345 ) /> <cfset DVD.setPrice( 9.99 ) /> <cfset DVD.setTitle( "The Shawshank Redemption" ) /> <cfset DVD.setDescription( "Andy finds that survival comes down to a simple choice: get busy living or get busy dying." ) /> <cfset DVD.setRegion( 2 ) /> <cfset DVD.setDuration( 2.16 ) /> <!--- create a Book ---> <cfset Book = CreateObject( "component", "BookProduct" ).init() /> <cfset Book.setSku( "67890" ) /> <cfset Book.setPrice( 30.99 ) /> <cfset Book.setTitle( "ColdFusion 8 Developer Tutorial" ) /> <cfset Book.setDescription( "This book is the most intense guide to creating professional ColdFusion applications available." ) /> <cfset Book.setIsbn( "978-1847194121" ) /> <cfset Book.setPages( 400 ) /> <cfoutput> <!--- show DVD ---> Product: #DVD.getTitle()# (Region #DVD.getRegion()#)<br /> £#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 /> £#NumberFormat( Book.getPrice(), "9.99" )#<br /> #Book.getDescription()#<br /> ISBN: #Book.getSku()# </cfoutput>