Computer Science Canada Interface Inheritance |
Author: | wtd [ Tue Aug 23, 2005 3:38 am ] | ||||||||||
Post subject: | Interface Inheritance | ||||||||||
In many object-oriented languages interface inheritance is a key aspect of the typing system. We can use interfaces to allow classes which don't share a meaningful common ancestor to be passed to the same method. For instance, we may have the CarnivalWorker and Dog classes. These two share no classic inheritance relationship. However, both can bark. Let's look at some C# code.
But in Ruby we don't impose such limitations of method arguments, so there's no need for this, right? Well, strictly speaking, no, but it can be very handy to know this relationship. So, how can we implement interfaces? Well, we'll use modules. Normally modules are used to compartmentalize methods. The most common example is the enumerable module which provides all kinds of handy looping methods. The methods in a module do have an implementation, and not just a signature. However, we can then override this implementation to provide the functionality we want. The default implementation can be to throw a meaningful error message.
We can see the inheritance relationship at the interactive interpreter easily enough.
We can also see what happens if we have a class that includes the Barker method, but doesn't override the method.
|
Author: | Cervantes [ Tue Aug 23, 2005 8:04 am ] |
Post subject: | |
Thanks wtd! I thought you said though that Ruby uses mix-ins. wtd wrote: Ruby and Java only support multiple inheritance via mix-ins and interface inheritance, respectively.
Or is that backwards? If not, would you so kindly explain these as well? |
Author: | rizzix [ Tue Aug 23, 2005 12:27 pm ] |
Post subject: | |
what in the world is mix-ins ??? |
Author: | wtd [ Tue Aug 23, 2005 1:59 pm ] | ||
Post subject: | |||
rizzix wrote: what in the world is mix-ins ???
A very powerful way of programming. The most powerful, mix-in, in my opinion, is Enumerable. Essentially, with a mix-iin you look at a chunk of methods that are common to many classes. So far this is like interface inheritance. However, in this case we provide implementations. In order to do this, we need to figure out what basic functionality each class that includs the mix-in must implement. In the case of enumerable, it's the "each" method. Let's look at a disturbingly simple example.
Now, I didn't write any code for collect, to_a, select, or reject, but I'm using them anyway. |
Author: | Cervantes [ Tue Aug 23, 2005 3:51 pm ] |
Post subject: | |
I must be missing something, because I don't see how that is any different than the code in the first post. Both have a module which is included into the class, thus allowing methods from the module to be integrated into the class. But the top post is supposedly Interface Inheritance, whereas this one is a mix-in? I pictured a mix-in as allowing the programmer to choose which methods to pull from a module. |
Author: | wtd [ Tue Aug 23, 2005 4:18 pm ] |
Post subject: | |
The first is a mix-in, but by having the default implementation be to raise an exception, we get an exception anytime we try to use one of those methods in a class that includes the module but doesn't override the method. |
Author: | rizzix [ Tue Aug 23, 2005 4:47 pm ] |
Post subject: | |
i see problems if u include more than one module having the same method name -,- |
Author: | wtd [ Tue Aug 23, 2005 5:05 pm ] | ||||||
Post subject: | |||||||
This is reasonably easy to understand. Methods can be changed at any time.
But we can keep the old method around with "alias".
Naturally, this extends to working with modules.
|