in reply to Understanding what Inheriting is.
OK, "how to use it" and "how does it work" are very different questions. I'll leave the second point to monks who understand the internals of Perl better than I, and just try to give you a feel for the motivation behind inheritance.
Inheritance is a programming construct that's supported directly in object-oriented languages (such as Smalltalk) and can be retrofitted into some others (such as Perl). Basically, it addresses the situation where you've found some code that does almost everything that you need, but not entirely. In that situation, you have some choices:
The first technique is usually not available if somebody's paying you to write the code -- although if it's available, it may be the most expedient way of getting what you want done!
The second usually creates maintenance headaches because it breaks the link to the origignal. For example, suppose the original author finds a bug and publishes a fix. Now the person maintaining your code has to find out about the bug, then figure out whether and how the fix applies to your modified version. Or a spiffy new version comes out and now you have copy and paste all over again. (Many other scenarios are possible, but I just want to give you the flavor.)
The third option is what inheritance does. It lets you create new code that looks and acts like the old code (as a matter of fact, it calls the old code!), except where you add new stuff.
For example, say you found a great (Perl module, Java class, C++ library) called SuperCool that does 98% of what you want, but you just need to add a couple of things. With inheritance, what you can do is create a class that inherits from SuperCool called, say, HyperCool. This one has all the methods of SuperCool, and the ones you added, but you don't have to duplicate the code for SuperCools's methods. The languages that support OOP will figure out if a call to a method in HyperCool is actually for code written in SuperCool, and will dispatch to that code with the calling client program being none the wiser. The net result is that you're able to provide SuperCool's functionality, and add to it, in a maintenance-friendly way.
There is a lot more that can be said about the topic. This is a miniminal overview, but hopefully it'll allow you to understand the more advanced material.
HTH.
|
|---|