Using Inheritance
Inheritance can be used when an object "IS A" more specific version of another object
<cfcomponent output="false" hint="I represent a generic 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="BookProduct" output="false" hint="I am the constructor"> <cfset super.init() /> <cfreturn this /> </cffunction> <!--- public methods ---> <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="DVDProduct" output="false" hint="I am the constructor"> <cfset super.init() /> <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()# <hr /> <!--- show Book ---> Product: #Book.getTitle()# (#Book.getPages()# Pages)<br /> £#NumberFormat( Book.getPrice(), "9.99" )#<br /> #Book.getDescription()#<br /> ISBN: #Book.getISBN()# </cfoutput>