Traits: Composable Units of Behaviour

  • Nathanael Schärli
  • Stéphane Ducasse
  • Oscar Nierstrasz
  • Andrew P. Black
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 2743)


Despite the undisputed prominence of inheritance as the fundamental reuse mechanism in object-oriented programming languages, the main variants—single inheritance, multiple inheritance, and mixin inheritance—all suffer from conceptual and practical problems. In the first part of this paper, we identify and illustrate these problems. We then present traits, a simple compositional model for structuring object-oriented programs. A trait is essentially a group of pure methods that serves as a building block for classes and is a primitive unit of code reuse. In this model, classes are composed from a set of traits by specifying glue code that connects the traits together and accesses the necessary state. We demonstrate how traits overcome the problems arising from the different variants of inheritance, we discuss how traits can be implemented effectively, and we summarize our experience applying traits to refactor an existing class hierarchy.


Inheritance mixins multiple inheritance traits reuse Smalltalk 


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Achermann, F., Nierstrasz, O.: Explicit Namespaces. In: Weck, W., Gutknecht, J. (eds.) JMLC 2000. LNCS, vol. 1897, pp. 77–89. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  2. 2.
    America, P.: Designing an object-oriented programming language with behavioural subtyping. In: Proceedings REX/FOOLS Workshop, Noordwijkerhout (June 1990)Google Scholar
  3. 3.
    Ancona, D., Lagorio, G., Zucca, E.: Jam - a smooth extension of java with mixins. In: Bertino, E. (ed.) ECOOP 2000. LNCS, vol. 1850, pp. 145–178. Springer, Heidelberg (2000)Google Scholar
  4. 4.
    Bergel, A., Ducasse, S., Wuyts, R.: Classboxes: A minimal module model supporting local rebinding. In: Proceedings of the Joint Modular Languages Conference 2003. Springer, Heidelberg (2003) (to appear)Google Scholar
  5. 5.
    Black, A., Hutchinson, N., Jul, E., Levy, H.: Object structure in the Emerald system. In: Proceedings OOPSLA 1986, ACM SIGPLAN Notices, vol. 21, pp. 78–86 (November 1986)Google Scholar
  6. 6.
    Black, A., Schärli, N., Ducasse, S.: Applying traits to the Smalltalk collection hierarchy. Technical Report IAM-02-007, Institut für Informatik, Universität Bern, Switzerland (November 2002), Also available as Technical Report CSE-02-014, OGI School of Science & Engineering, Beaverton, Oregon, USAGoogle Scholar
  7. 7.
    Borning, A.H., Ingalls, D.H.H.: Multiple inheritance in Smalltalk-80. In: Proceedings at the National Conference on AI, Pittsburgh, PA (1982)Google Scholar
  8. 8.
    Bouraqadi-Saadani, N.M.N., Ledoux, T., Rivard, F.: Safe metaclass programming. In: Proceedings OOPSLA 1998, pp. 84–96 (1998)Google Scholar
  9. 9.
    Bracha, G.: The Programming Language Jigsaw: Mixins, Modularity and Multiple Inheritance. Ph.D. thesis, Dept. of Computer Science, University of Utah (March 1992)Google Scholar
  10. 10.
    Bracha, G., Cook, W.: Mixin-based inheritance. In: Proceedings OOPSLA/ ECOOP 1990, ACM SIGPLAN Notices, October 1990, vol. 25, pp. 303–311 (1990)Google Scholar
  11. 11.
    Cook, S.: OOPSLA 1987 Panel P2: Varieties of inheritance. In: OOPSLA 1987 Addendum To The Proceedings, pp. 35–40. ACM Press, New York (1987)Google Scholar
  12. 12.
    Cook, W.R.: Interfaces and specifications for the Smalltalk-80 collection classes. In: Proceedings OOPSLA 1992, ACM SIGPLAN Notices, vol. 27, pp. 1–15 (October 1992)Google Scholar
  13. 13.
    Curry, G., Baer, L., Lipkie, D., Lee, B.: TRAITS: an approach to multiple inheritance subclassing. In: Proceedings ACM SIGOA, Newsletter, vol. 3, Philadelphia (June 1982)Google Scholar
  14. 14.
    Dixon, R., McKee, T., Vaughan, M., Schweizer, P.: A fast method dispatcher for compiled languages with multiple inheritance. In: Proceedings OOPSLA 1989, ACM SIGPLAN Notices, vol. 24, pp. 211–214 (October 1989)Google Scholar
  15. 15.
    Ducournau, R., Habib, M., Huchard, M., Mugnier, M.L.: Monotonic conflict resolution mechanisms for inheritance. In: Proceedings OOPSLA 1992, ACM SIGPLAN Notices, vol. 27, pp. 16–24 (October 1992)Google Scholar
  16. 16.
    Ducournau, R., Habib, M.: On some algorithms for multiple inheritance in object-oriented programming. In: Bézivin, J., Hullot, J.-M., Lieberman, H., Cointe, P. (eds.) ECOOP 1987. LNCS, vol. 276, pp. 243–252. Springer, Heidelberg (1987)CrossRefGoogle Scholar
  17. 17.
    Duggan, D., Techaubol, C.-C.: Modular mixin-based inheritance for application frameworks. In: Proceedings OOPSLA 2001, pp. 223–240 (October 2001)Google Scholar
  18. 18.
    Flatt, M., Krishnamurthi, S., Felleisen, M.: Classes and mixins. In: Proceedings of the 25th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, pp. 171–183. ACM Press, New York (1998)CrossRefGoogle Scholar
  19. 19.
    Goldberg, A., Robson, D.: Smalltalk 80: the Language and its Implementation. Addison Wesley, Reading (1983)zbMATHGoogle Scholar
  20. 20.
    Graube, N.: Metaclass compatibility. In: Proceedings OOPSLA 1989, ACM SIGPLAN Notices, vol. 24, pp. 305–316 (October 1989)Google Scholar
  21. 21.
    Guttag, J.V., Horning, J.J., Wing, J.M.: The larch family of specification languages. IEEE Transactions on Software Engineering 2(5), 24–36 (1985)Google Scholar
  22. 22.
    Ingalls, D., Kaehler, T., Maloney, J., Wallace, S., Kay, A.: Back to the future: The story of Squeak, A practical Smalltalk written in itself. In: Proceedings OOPSLA 1997, pp. 318–326 (November 1997)Google Scholar
  23. 23.
    Keene, S.E.: Object-Oriented Programming in Common-Lisp. Addison Wesley, Reading (1989)zbMATHGoogle Scholar
  24. 24.
    Kniesel, G.: Type-safe delegation for run-time component adaptation. In: Guerraoui, R. (ed.) ECOOP 1999. LNCS, vol. 1628, pp. 351–366. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  25. 25.
    LaLonde, W., Pugh, J.: Subclassing ≠ Subtyping ≠ Is-a. Journal of Object-Oriented Programming 3(5), 57–62 (1991)Google Scholar
  26. 26.
    Madsen, O.L., Magnusson, B., Moller-Pedersen, B.: Strong typing of object-oriented languages revisited. In: Proceedings OOPSLA/ECOOP 1990, ACM SIGPLAN Notices, vol. 25, pp. 140–150 (October 1990)Google Scholar
  27. 27.
    Mens, T., van Limberghen, M.: Encapsulation and composition as orthogonal operators on mixins: A solution to multiple inheritance problems. Object Oriented Systems 3(1), 1–30 (1996)Google Scholar
  28. 28.
    Meyer, B.: Eiffel: The Language. Prentice-Hall, Englewood Cliffs (1992)zbMATHGoogle Scholar
  29. 29.
    Meyer, B.: Object-Oriented Software Construction, 2nd edn. Prentice-Hall, Englewood Cliffs (1997)zbMATHGoogle Scholar
  30. 30.
    Mezini, M.: Dynamic object evolution without name collisions. In: Aksit, M., Matsuoka, S. (eds.) ECOOP 1997. LNCS, vol. 1241, pp. 190–219. Springer, Heidelberg (1997)CrossRefGoogle Scholar
  31. 31.
    Mezini, M., Ostermann, K.: Integrating independent components with ondemand remodularization. In: Proceedings OOPSLA 2002, pp. 52–67 (November 2002)Google Scholar
  32. 32.
    Moon, D.A.: Object-oriented programming with flavors. In: Proceedings OOPSLA 1986, ACM SIGPLAN Notices, vol. 21, pp. 1–8 (November 1986)Google Scholar
  33. 33.
    Sakkinen, M.: Disciplined inheritance. In: Cook, S. (ed.) Proceedings ECOOP 1989, Nottingham, pp. 39–56. Cambridge University Press, Cambridge (1989)Google Scholar
  34. 34.
    Sakkinen, M.: The darker side of C++ revisited. Structured Programming 13(4), 155–177 (1992)Google Scholar
  35. 35.
    Schaffert, C., Cooper, T., Bullis, B., Killian, M., Wilpolt, C.: An Introduction to Trellis/Owl. In: Proceedings OOPSLA 1986, ACM SIGPLAN Notices, vol. 21, pp. 9–16 (November 1986)Google Scholar
  36. 36.
    Schärli, N., Black, A.: A browser for incremental programming. Technical Report CSE-03-008, OGI School of Science & Engineering, Beaverton, Oregon, USA (April 2003)Google Scholar
  37. 37.
    Schärli, N., Nierstrasz, O., Ducasse, S., Wuyts, R., Black, A.: Traits: The formal model. Technical Report IAM-02-006, Institut für Informatik, Universität Bern, Switzerland (November 2002), Also available as Technical Report CSE-02-013, OGI School of Science & Engineering, Beaverton, Oregon, USAGoogle Scholar
  38. 38.
    Snyder, A.: Encapsulation and inheritance in object-oriented programming languages. In: Proceedings OOPSLA 1986, ACM SIGPLAN Notices, vol. 21, pp. 38–45 (November 1986)Google Scholar
  39. 39.
    Snyder, A.: Inheritance and the development of encapsulated software systems. In: Research Directions in Object-Oriented Programming, pp. 165–188. MIT Press, Cambridge (1987)Google Scholar
  40. 40.
    Steele, G.L.: Common Lisp The Language, 2nd edn. Digital Press (1990) (book)Google Scholar
  41. 41.
    Stroustrup, B.: The C++ Programming Language. Addison Wesley, Reading (1986)zbMATHGoogle Scholar
  42. 42.
    Stroustrup, B.: The Design and Evolution of C++. Addison-Wesley, Reading (1994)Google Scholar
  43. 43.
    Sweeney, P.F., Gil, J. (Yossi): Space and time-efficient memory layout for multiple inheritance. In: Proceedings OOPSLA 1999, pp. 256–275. ACM Press, New York (1999)CrossRefGoogle Scholar
  44. 44.
    Taivalsaari, A.: On the notion of inheritance. ACM Computing Surveys 28(3), 438–479 (1996)CrossRefGoogle Scholar
  45. 45.
    Ungar, D., Smith, R.B.: Self: The power of simplicity. In: Proceedings OOPSLA 1987, ACM SIGPLAN Notices, vol. 22, pp. 227–242 (December 1987)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2003

Authors and Affiliations

  • Nathanael Schärli
    • 1
  • Stéphane Ducasse
    • 1
  • Oscar Nierstrasz
    • 1
  • Andrew P. Black
    • 2
  1. 1.Software Composition GroupUniversity of BernSwitzerland
  2. 2.OGI School of Science & EngineeringOregon Health and Science University 

Personalised recommendations