4 Replies Latest reply on Jul 17, 2007 12:10 PM by Newsgroup_User

    OOP "inheritance"

    Chuck1411 Level 1

      I have component base class, I'll call Obj1
      and I have another component parent class that extends Obj1, I'll call Obj2.

      I have two functions in each, both with same names in each using function overriding.
      In Obj1
      Function A calls Function B

      In Obj2
      Function A calls Function B
      Function B calls Function C
      Function C calls Super.A() which is (Obj1->A)

      From there Obj1->A should call Obj1->B, but it isn't. It's calling Obj2->B, and thus I get an infinite loop. How is it that Obj1 even knows about Obj2?

      I was originally calling from Obj1 <cfset B()> and I tried <cfset this.B()> but both get the same results.

      I know I've tried this a while ago when using Application inheritance for sub-modules, and I was getting different behavior when I wanted to do a refresh onRequestStart with URL.request and calling sub-modules OnSessionStart and OnApplicationStart, but it was only calling the base Application. I'm a bit confused on why the change in behavior, or if I'm not understanding things.
        • 1. Re: OOP &quot;inheritance&quot;
          Level 7
          > I have component base class, I'll call Obj1

          This is where you perhaps start confusing yourself. Class != object. A
          class defines what it is to be an object; an object is a "realisation" of
          that class.

          In CF there are no classes, but the closest parallel is a CFC file. Not a
          CFC *instance*, but the file itself, in the file system.

          Similarly in CF there aren't "objects", really, but the closest parallel is
          a CFC instance (as created by createObject(), <cfobject> or <cfinvoke>).
          Objects reside in memory, not in the file system.


          > From there Obj1->A should call Obj1->B, but it isn't. It's calling Obj2->B,

          No, because if you have this:

          o = create("component", "Obj2");

          then o is an Obj2. It's not "part Obj2 and part Obj1, depending on which
          line of code is being executed". It doesn't matter whether the methods are
          defined in Obj2 or Obj1; the object itself is an Obj2.

          When you call a method, CF doesn't look for a method of that name *in the
          current CFC file*, it looks for a method of that name *in the object*. And
          because it's an Obj2, it will "see" the b() method of Obj2, even if the
          code being executed is in Obj1.cfc.

          Make sense?

          --
          Adam
          • 2. Re: OOP &quot;inheritance&quot;
            Chuck1411 Level 1
            Yes thanks for the clarification.

            I guess its back to the drawing board ... tomorrow.
            • 3. Re: OOP &quot;inheritance&quot;
              Chuck1411 Level 1
              Follow on question:

              Does then Super.function() only work on 'top' object?

              Example: (using above names but adding a Obj3)
              Obj1->A()
              - does something

              Obj2->A()
              - calls Super.A()

              Obj3->A()
              - calls Super.A()

              Does this work as intended?
              • 4. Re: OOP &quot;inheritance&quot;
                Level 7
                My usual advice when people ask questions like this is...

                ... did you try it? Did it seem to work? If it didn't seem to work, in
                what way did it seem to not work?

                And a bit of reading is always helpful too:
                http://livedocs.adobe.com/coldfusion/7/htmldocs/00001059.htm

                --
                Adam