Does JavaFX have Multiple Inheritance?


CatDog cartoon from Nickodean

CatDog cartoon from Nickelodeon

Introduction

Often when we learn about object oriented programming we try to find out if a particular language contains a dirty little phrase called multiple inheritance. Of course there are good reasons to try to avoid it (Diamond Problem) but sometimes you can’t.  I believe that it can be used in certain contexts which can be done safely (fearing and trembling from angry purists). In Java when asked if multiple inheritance is supported the answer is “Well, Sort of” or “Yes, but…“. Java has interfaces which follows the Design by Contract concept which forces the developer to implement the interface methods. Fundamentally when we think of abstraction we think of what behavior is common across classes. Abstract classes help but tend to get bloated and some folks resort to creating NOP (No operation performed) methods. One of the major principles of object oriented programming is re-use not un-re-use while rendering an object very coupled-ygoop. It would be wonderful if you could have method re-use from an abstract data type. Well, JavaFX has this ability to do this very thing! So, to ask the question again “Does JavaFX have multiple inheritance?“. I would answer it like this: “Yes, its similar to Java’s Interfaces but with implementation details“. JavaFX uses a concept called Mixin classes which allows the developer to inherit multiple classes. Sweet!

Example

I couldn’t help but think about all kinds of cats when thinking about multiple inheritance (not to be confused with these types of cats I Can Has Cheezburger ). In a nutshell there are Big cats and Small cats.

Requirements:

  • Some Big cats can roar, but all big cats can’t meow.
  • Similarly small cats can meow, but all small cats can’t roar.
  • All cats can purr.

In this example we will model reusable behavior as Mixins and we will also create the standard abstract classes such as “Animal” and “Cat“.

Step 1: Create Abstract Classes

abstract class Animal {
    public var name:String;
    public function eat(food:String){
        println("munchin on {food}");
    }
}

abstract public class Cat extends Animal {
    public function purr(volume:Integer){
        println("Purrrr...level({volume})");
    }
    public function scratch(){
        println("Scratching...")
    }

}

Step 2: Create Mixin Classes

public mixin class Roarer {
    public var bass:Integer = 20;
    public function roar(){
        println("Roar...");
    }
}

public mixin class Meower {
    public var pitch:Integer = 5;
    public function meow(){
        println("Meow...");
    }
}

Step 3: Create Derived Classes Inheriting Mixin Classes

// Cheetah's can't roar and can't meow
class Cheetah extends Cat {
}

class Lion extends Cat, Roarer{
    public override var bass = 50;
    public override function roar() {
        println("(ROAR) I'm King of the land!!!");
    }
}

class HouseCat extends Cat, Meower {
    public override var pitch = 10;
    public override function meow(){
        println("Meow...");
    }
}

Step 4: Create instances of the types of Cats

var fluffy:Cat = HouseCat{name:'fluffy'}; // regular house cat
var simba:Cat = Lion{name:'Simba'};      // Lion King cat
var chester:Cat = Cheetah{name:'Chester'}; // Cheetos' Chester

var cats:Cat[] = [fluffy, simba, chester]; // cat bus

Step 5: Create a script level function

Cat will ask to come into your house. Notice the down cast to Meower and Roarer type cats.

function letMeIntoHouse(cat:Cat){
        print("LET ME IN! ");
        if (cat instanceof Meower){
            var meower = (cat as Meower);
            meower.meow();
        }
        if (cat instanceof Roarer){
            var roarer = (cat as Roarer);
            roarer.roar();
        }
        cat.scratch();
}

Step 6: run() function like Java’s main()

This will loop through a sequence of cats by:

  • Cat asking in your house
  • Introduces cat
  • Feeds cat
  • Cat will purr
  • function run(){
        def food:String = "tender vittles";
        for (cat:Cat in cats){
            letMeIntoHouse(cat);
            print("The cat named {cat.name} is ");
            cat.eat(food);
            cat.purr(5);
        }
    }

    Output

    LET ME IN! Meow...
    Scratching...
    The cat named fluffy is munchin on tender vittles
    Purrrr...level(5)
    LET ME IN! (ROAR) I'm King of the land!!!
    Scratching...
    The cat named Simba is munchin on tender vittles
    Purrrr...level(5)
    LET ME IN! Scratching...
    The cat named Chester is munchin on tender vittles
    Purrrr...level(5)

    Conclusion

    Although this example doesn’t use Mixins in an RIA context, instead its used with simple domain objects to show multiple inheritance. Hopefully we can now answer the question when asked, “Does JavaFX have multiple inheritance?“. Is it ever a quick answer?

    References:

    Advertisements

    5 thoughts on “Does JavaFX have Multiple Inheritance?

    1. PhiLho

      Good presentation.
      I still wonder what is the difference between a mixin and an abstract class, beside the artificial distinction that you cannot inherit from multiple abstract classes (you could in versions 1.1 and before!).

      JavaFX solved the diamond problem by allowing to specify which mixin is used when accessing a common member or field.
      I gave an example in Mixin conflict: bug or feature? which also shows a bug (reported as RT-5033 in Jira / Kenai).

      This multiple inheritance, if not abused, is nice as it shows the various facets/roles of an object, while providing some boilerplate code.

    2. Josh Marinacci

      Nice job. When people ask about mixins in JavaFX I usually say: Mixins give you 90% of what you want out of true multiple inheritance but without the nasty edge cases that cause problems. Plus it’s faster!

    3. carldea Post author

      Josh,
      Thanks!
      I’ll have to use that response also. I believe a developer should try to be more abstract with interfaces but if they ever need multiple inheritance its so easy to re-factor them into Mixins. Also when re-factoring during these situations the rule of three comes to mind. I think I’m addicted to Mixins and may never use Abstract classes again. (well…)

    4. carldea Post author

      PhiLho,

      Thanks!
      Regarding the difference between Mixins and abstract classes I believe from a JavaFX Script perspective the difference is that a Mixin is not an abstract class. Sounds like a silly answer.. yeah I know, but wait. Let me try to explain. Telling you what its not may be easier for me to explain. I’m still learning so here is what I think (hopefully a Sun engineer can validate my claim):
      A JavaFX class can extend at most only one Java or JavaFX class. So, in the JavaFX Script world the language says you can extend at most one Java abstract class or one JavaFX abstract class so (there can only be one). I’m not sure, but I bet in Java byte code an abstract JavaFX class is really an abstract Java class under the covers. Imho I think about Java to JavaFX interoperability to happen and the need for Mixins(Multi-Inheritance) this helps preserve the Java Abstract class between two languages.
      [DISCLAIMER: Excuse me… I’ve modified my statements a few times. I just don’t want to misinform folks with bad explanations.]

      JavaFX class can extend any number of Java interfaces and any number of JavaFX mixin classes.

      That’s why I would answer the question the same..“Yes, its similar to Java’s Interfaces but with implementation details“

      I hope that helps!
      -Carl

    5. Pingback: Java desktop links of the week, July 6 | Jonathan Giles

    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