O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extending Classes

You can extend a Java class from a Scala class, and vice versa. For the most part, this should just work. As discussed earlier, if your methods accept function values as parameters, you’ll have trouble overriding them. Exceptions are also a problem.

Scala doesn’t have the throws clause. In Scala you can throw any exception from any method without having to explicitly declare that as part of the method signature. However, if you override such a method in Java, you’ll run into trouble when you try to throw an exception. Let’s look at an example. Suppose we have a Bird class defined in Scala:

 
abstract​ ​class​ Bird {
 
def​ fly()
 
//...
 
}

We also have another class, Ostrich:

Intermixing/Ostrich.scala
 
class​ Ostrich ​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required