How to prevent corporate tracking

Over 55 percent of users polled believe you can’t remain anonymous on the Internet. People have become frustrated with reports on corporate tracking that most have become apathetic. Data on anyone…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Extending Classes

Pragmatic Scala — Pragmatic Programmers (120 / 140)

👈 Using Singletons and Companion Objects from Java | TOC | Wrapping Up 👉

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:

We also have another class, Ostrich:

where NoFlyException is defined like this:

In the previous code, Ostrich’s fly method was able to throw the exception without any problem. However, if we implement a nonflying bird in Java, we’ll run into trouble, as shown here:

First, if we simply throw the exception, Java will complain about an unreported exception being thrown. But if we declare the intention of throwing the exception with the throws clause, we’ll get this:

Even though Scala is flexible and doesn’t insist that you specify what exceptions you throw, if you intend to extend from those methods in Java, you’ll have to ask the Scala compiler to emit those details in the method signature. Scala provides a backdoor for that by defining the @throws annotation.

Add a comment

Related posts:

Getting Input

Crystal is for Ruby programmers who want more performance, or for developers who enjoy working in a high-level scripting environment. Crystal combines native execution speed and concurrency with Ruby-like syntax, so you will feel right at home. This book, the first available on Crystal, shows you how to write applications that h