Sunday, November 02, 2003

Traits and Multiple Inheritance

Multiple inheritance...A topic that makes my skin literally crawl! So, when I had two friends of mine tell me that traits smelled like multiple inheritance, I wondered why I didn't consider it to be. I think the reason is that traits are like Java interface classes, but with the ability to add behavior that is shared. Traits require the class using it to implement a certain number of methods that the trait needs. Traits provide a contract that the user of such must adhere to in order to get the behavior of the trait. In multiple inheritance, you're also getting state of the other class and it just feels more heavy weight. I imagine traits as lightweight objects that can be attached to your heavyweight class. The only problem I see is method clashes, but you could require the class using the trait to resolve that. I'm still looking into the issue myself (Hell, I've even been playing around with not using getters/setters all of the time) to see if they make sense. So far, I still like them (and I still like getters/setters for all instance variables--just mark them all private or protected in Java)!

No comments: