This page is no longer maintained — Please continue to the home page at www.scala-lang.org

inheritance vs composition

6 replies
wwagner4
Joined: 2011-08-11,
User offline. Last seen 1 year 2 weeks ago.

Because multiple inheritance is much less restrictive than single
inheritance I feel like I do not use composition any longer. It seems
to be possible to write code using the following rules.

1. Code all logic in traits
2. Mixin the traits you need in your classes

What are the arguments against programming by these rules?

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: inheritance vs composition


On Thu, Aug 11, 2011 at 8:41 AM, wwagner4 <wwagner4 [at] gmail [dot] com> wrote:
Because multiple inheritance is much less restrictive than single
inheritance I feel like I do not use composition any longer. It seems
to be possible to write code using the following rules.

1. Code all logic in traits
2. Mixin the traits you need in your classes

What are the arguments against programming by these rules?

Because you always need access to concrete types and constructors.

--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: inheritance vs composition

wwagner4 skrev 2011-08-11 08:41:
> Because multiple inheritance is much less restrictive than single
> inheritance I feel like I do not use composition any longer. It seems
> to be possible to write code using the following rules.
>
> 1. Code all logic in traits
> 2. Mixin the traits you need in your classes
>
> What are the arguments against programming by these rules?

Some arguments that pops into my head:

- No clean separation between class interface and implementation. When a
class inherit from an implementation trait you might inherit
implementation details that you don't want to expose in the class
interface. With composition you have full control what to expose in the
class interface.

- Dynamic composition is much harder. Consider this example:

trait Gui
class OpenGlGui extends Gui
class DirectXGui extends Gui

// Class using composition
class Display(val gui : Gui)

def createGui(config : String) : Gui = ...

def createDisplay(config : String) = new Display(createGui(config))

If you would instead use inheritance (i.e. "Display with OpenGlGui" and
"Display with DirectXGui") you can't separate the createGui and
createDisplay methods, i.e. it's less modularizable.

/Jesper Nordenberg

ARKBAN
Joined: 2011-08-11,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: inheritance vs composition

Composition also allows you to choose which members and how those
members are exposed from the inner classes. With inheritance you must
take all members at their declared visibility. I find such control very
useful when I'm composing objects to create a class that is different
than the sum of all the composed classes, or I want to use the composed
classes in very specific/controlled ways.

ARKBAN

On 8/11/2011 4:03 AM, Jesper Nordenberg wrote:
> wwagner4 skrev 2011-08-11 08:41:
>> Because multiple inheritance is much less restrictive than single
>> inheritance I feel like I do not use composition any longer. It seems
>> to be possible to write code using the following rules.
>>
>> 1. Code all logic in traits
>> 2. Mixin the traits you need in your classes
>>
>> What are the arguments against programming by these rules?
>
> Some arguments that pops into my head:
>
> - No clean separation between class interface and implementation. When
> a class inherit from an implementation trait you might inherit
> implementation details that you don't want to expose in the class
> interface. With composition you have full control what to expose in
> the class interface.
>
> - Dynamic composition is much harder. Consider this example:
>
> trait Gui
> class OpenGlGui extends Gui
> class DirectXGui extends Gui
>
> // Class using composition
> class Display(val gui : Gui)
>
> def createGui(config : String) : Gui = ...
>
> def createDisplay(config : String) = new Display(createGui(config))
>
> If you would instead use inheritance (i.e. "Display with OpenGlGui"
> and "Display with DirectXGui") you can't separate the createGui and
> createDisplay methods, i.e. it's less modularizable.
>
> /Jesper Nordenberg
>

Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: inheritance vs composition

On 11/08/2011 08:41, wwagner4 wrote:
> Because multiple inheritance is much less restrictive than single
> inheritance I feel like I do not use composition any longer. It seems
> to be possible to write code using the following rules.
>
> 1. Code all logic in traits
> 2. Mixin the traits you need in your classes
>
> What are the arguments against programming by these rules?

Isn't what dependency injection is about? Ie. avoiding hard-coded
dependencies on an implementation of some interface, to allow easy
switching of components?
Somebody having really used this paradigm can confirm or correct me...

Adam Jorgensen
Joined: 2011-04-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: inheritance vs composition
I think there is some overlap, but there is a such a thing as too much dependency injection ;-)

On 11 August 2011 15:51, Philippe Lhoste <PhiLho [at] gmx [dot] net> wrote:
On 11/08/2011 08:41, wwagner4 wrote:
Because multiple inheritance is much less restrictive than single
inheritance I feel like I do not use composition any longer. It seems
to be possible to write code using the following rules.

1. Code all logic in traits
2. Mixin the traits you need in your classes

What are the arguments against programming by these rules?

Isn't what dependency injection is about? Ie. avoiding hard-coded dependencies on an implementation of some interface, to allow easy switching of components?
Somebody having really used this paradigm can confirm or correct me...

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: inheritance vs composition
Just because Scala has cool features, doesn't mean that the principles of good programming are turned on their head. Strongly agree with Jesper and ARKBAN's objections.
Regards,Ken

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland