Andy Parker
Andy Parker

Reputation: 63

whats the difference between extending a class and implementing a trait in Groovy?

I'm just getting started with Groovy, I'm experimenting with using traits. Below I've pasted in a learning exercise from www.programmingbydoing.com these exercises were very useful when I was starting to learn Java so I'm using the for Groovy as well. I finished this class then thought, why wouldn't I just extend a class instead of using a trait? I'm stumped so if anyone could help, whats the difference between extending a class and implementing a trait in Groovy?

  class HowOldAreYou implements AskingAndAnswering {
    /**
     *
     * @param args
     */
    static void main(args) {
        def name = AskName()
        def age = AskAge(name)
        if (age < 16)       result "You can't drive yet $name"
        if (age < 18)       result "You can't vote yet $name"
        if (age < 25)       result "You can't rent a car yet $name"
        else if (age > 25)  result "You can do anything that is legal $name"
        else                result "That's an unusual age $name!"
    }
    /**
     *
     */
    trait AskingAndAnswering {
        static def keyboard = new Scanner(System.in)
        static String AskName() {
            println "What is your name?"
            def name = keyboard.next()
        }
        static int AskAge(name) {
            println "\nOk, $name How old are you?"
            int age = keyboard.nextInt()
        }
        static void result(answer) {println "\n $answer"}
    }
}

Upvotes: 6

Views: 1345

Answers (2)

J&#233;r&#233;mie B
J&#233;r&#233;mie B

Reputation: 11022

A Trait is not specific to Groovy. It's a concept in the object oriented programming "theory", and a lot of language implements this concept.

"In theory", a Trait is not a class. It doesn't represent "a thing", but more a "capability" or a "behavior". It's a set of methods and states which provide a capability.

This capability can be applied to a class ("a thing"), and a class can have multiple capabilities (can use multiple traits).

In Groovy, it's the main practical difference between class and trait (and of course the resolution of conflicts between traits and classes), but it's more important to think about the concept of a trait, that the technical implementation of it.

So, yes, if you have only one trait, you can maybe use an abstract class, but you should keep in mind what is your trait.

Upvotes: 3

pczeus
pczeus

Reputation: 7867

By rule you can only extend one class. With traits you use the `implements', similar to interfaces. There no limit to the number of traits your 'implement'.

But a trait is different from an interface, as it contains fully implemented methods. The result is very much like composition, where you can add functionality to an existing class, as needed, by simply implementing a reusable trait.

This behavior becomes very similar to providing 'multiple interitance`, since you can effectively 'extend' the behavior of a class with multiple traits, that can act similar to a base class.

Upvotes: 1

Related Questions