Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Private Methods Meditation

by tilly (Archbishop)
on Jul 19, 2004 at 16:04 UTC ( [id://375594]=note: print w/replies, xml ) Need Help??


in reply to Re: Private Methods Meditation
in thread Private Methods Meditation

If you're inheriting from someone else, you're mucking around with their internals.

If you're uncomfortable doing so, then you shouldn't be inheriting.

Replies are listed 'Best First'.
Re^3: Private Methods Meditation
by Ovid (Cardinal) on Jul 19, 2004 at 18:02 UTC

    tilly, you have a tendency to toss out statements like that without explaining what you mean (that's not intended as criticism.) Are you stating that all OO languages are like this? Clearly you are referring to more than just a blessed referent. With much tighter encapsulation available with Java or C++, do you mean even these languages have the programmer automatically mucking with internals upon subclassing or is this a peculiarity of Perl (or dynamically-typed languages)?

    Cheers,
    Ovid

    New address of my CGI Course.

      I was stating a general rule for all OO languages. There may be additional traps in Perl, but it is true in general.

      When I declare a subclass, I'm saying that A isa B. Making this work generally requires that A and B cooperate to some extent. At a minimum, the author of A needs to know what methods B is likely to expect to see overridden. Missing some of them may mean that your object behaves weirdly. Sometimes you need access to class data. In many languages the subclass implementor has access to special protected functions, and needs to know when they are supposed to be called. All of this may need updating if the parent module changes. And so on.

      In short, the concept of "isa" represents reasonably tight coupling between components. Some of the tightness is accidental - such as the result of the visibility of the implementation in Perl. But some is intrinsic to what OO is. If you're unwilling to accept this coupling, then you probably shouldn't be subclassing.

      Remember that OO provides a series of potential compromises between reducing duplication of information and reducing coupling. It is good to not duplicate information. It is good to have loose coupling. Those goals are good because each avoids a difficult problem. Unfortunately they naturally conflict, reducing duplicate information naturally couples things together.

      Among those potential compromises, inheritance is the choice that gives the best opportunity for removing duplication of information. But at a natural cost of tighter coupling.

      Can't speak for tilly, but it's defaintely something that effects pretty much any language, though Perl might be worse off than most. If you have a method in your subclass, you always run the risk that the parent class will define a method with the same name, but with very different functionality.

      I don't remember how the method is searched for in Java, but I believe that if you call that method in the parent, then it calls the parent class's version. This isn't true in Perl without some tricks.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

        If you have a method in your subclass, you always run the risk that the parent class will define a method with the same name, but with very different functionality.

        Not so. In C++, a method must be explicitly declared as virtual if you wish to allow it to be overridden in subclasses. As a result, a if you call a superclass method and that method calls the method foo(), it will only grab the subclass foo() if the superclass had declared foo() as virtual. (At least, that's my understanding. C++ is not my strong suit.)

        In Java, you can declare a method as private and subclasses cannot override that method.

        I'm still not sure how subclassing messes with something's internals, though. In Perl, it's pretty much a given that this will happen since there is so little support for encapsulation. This ties in with my comments above. I don't know if tilly meant all OO languages.

        Cheers,
        Ovid

        New address of my CGI Course.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://375594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found