August 2013
Intermediate to advanced
720 pages
16h 23m
English
You want to implement a Java interface in a Scala application.
In your Scala application, use the extends and with keywords to implement your Java
interfaces, just as though they were Scala traits.
Given these three Java interfaces:
// javapublicinterfaceAnimal{publicvoidspeak();}publicinterfaceWagging{publicvoidwag();}publicinterfaceRunning{publicvoidrun();}
you can create a Dog class in
Scala with the usual extends and
with keywords, just as though you
were using traits:
// scalaclassDogextendsAnimalwithWaggingwithRunning{defspeak{println("Woof")}defwag{println("Tail is wagging!")}defrun{println("I'm running!")}}
The difference is that Java interfaces don’t implement behavior, so if you’re defining a class that extends a Java interface, you’ll need to implement the methods, or declare the class abstract.
Read now
Unlock full access