Multiple Inheritence In Scala with Trait & Mixin

Java does not support multiple inheritance. Scala is different . It usages scala traits and mixin (http://en.wikipedia.org/wiki/Mixin) concept to implement multiple inheritance. First check what is traits.

There are  three constructs in Java Class, Interface and Abstract class. Trait a new construct defined in Scala that lies halfway between an interface and a class. Traits are unusual in that a class can incorporate as many of them as desired, like interfaces, but they can also contain behaviour, like classes. In some way we can assume trait as abstract class with the additional property that is used in multiple inheritance. Also, like both classes and interfaces, traits can introduce new methods. But unlike either, the definition of that behaviour isn’t checked until the trait is actually incorporated as part of a class. Or, put differently, you can define methods that aren’t checked for correctness until they’re incorporated into a trait-using class definition.

Lets check how to implement multiple inheritance with traits and mixin concept:-

Create the base abstract class:-

scala> abstract class  Bird{

| def sound:String

| }

Create two traits each one overriding sound behaviour  with some additional behaviour specific to the subclass:-

scala> trait FlyingBird extends Bird{

| override def sound = "Flying Sound"

| def work () = { "Flying Flying"}

| }

Here we extends the abstract class and overridden sound behaviour and added one work method.

Create one more subclass:-

scala> trait RunningBird extends Bird{

| override def sound = "Running Sound"

| def run() = "Running Running"

| }

Here also we extends the abstract class and overridden sound behaviour and added one run method.

Now try to create the third class which extends both of the above subclass.

Here we get the error. Because the compile could not understand which sound behaviour to take.

scala> class FlyingRunningBird extends FlyingBird extends RunningBird

<console>:1: error: ';' expected but 'extends' found.

class FlyingRunningBird extends FlyingBird extends RunningBird

^

Now use mixin concept with “with”  keyword:-

scala> class FlyingRunningBird extends FlyingBird  with RunningBird

defined class FlyingRunningBird

What does it mean that we are creating FlyingRunningBird with FlyingBird  and the common behaviour is overridden by RunningBird. We can assume that we are replacing the common behaviours with the constructs come after with keyword. So the FlyingRunningBird will get common behaviour (ie sound() from RunningBird).

Lets check this:-

Create one object flyRunBird.

scala> var flyRunBird = new FlyingRunningBird

flyRunBird: FlyingRunningBird = FlyingRunningBird@1f2a9da

scala> flyRunBird.work

res0: java.lang.String = Flying Flying

There should not be any confusion as it takes work from FlyingBird class.

scala> flyRunBird.run

res1: java.lang.String = Running Running

There should not be any confusion as it takes work from RunningBird class.

scala> flyRunBird.sound

res2: java.lang.String = Running Sound

Here is the point. As we have written “with RunningBird” so common behaviours(ie sound) is taken from  RunningBird class hence we get sound behaviour is taken from RunningBird.

Lets change the with keyword, and check the behaviour:-

First create a class:-

scala> class RunningFlyingBird extends RunningBird with FlyingBird

defined class RunningFlyingBird

Create an object of RunningFlyingBird.

scala> var runFlyBird = new RunningFlyingBird

runFlyBird: RunningFlyingBird = RunningFlyingBird@125b750

scala> runFlyBird.work

res3: java.lang.String = Flying Flying

This is expected as we have only one implementation of work in the inheritance  hierarchy.

scala> runFlyBird.run

res4: java.lang.String = Running Running

This is expected as we have only one implementation of run in the inheritance  hierarchy.

scala> runFlyBird.sound

res5: java.lang.String = Flying Sound

Now, here  as “with FlyingBird” is written, so common behaviours(i.e. sound) is taken from  FlyingBird class hence we get sound behaviour is taken from FlyingBird.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s