Konstantin Milyutin
Konstantin Milyutin

Reputation: 12366

Compile file containing java and scala code

Ìs it possible to compile file that contains both scala and java code? I'm doing online course and was given java source files, which I need to extend. Do I have to use only Java in already existing files?

Upvotes: 3

Views: 240

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

You cannot have both Java and Scala in one file. After all it's either .java or .scala.

However you are free to:

  • extend Java classes in Scala (and vice-versa)
  • implement Java interfaces in Scala (syntax is the same as for extending traits)
  • extend Scala traits in Java (syntax is the same as for implementing Java interfaces)

...as long as they are in different files.

Upvotes: 9

Related Questions