How to deal with Scala x Java classcasting

Asked

Viewed 32 times

1

I’m having a problem working with heritage between Scala and Java.

To contextualize, follow an example that reflects my problem.

  • Java Class
public class Animal {
}
  • Java Class
public class Cat extends Animal {
}
  • Scala class
class Dog extends Animal {
}
  • Java class
class Main {
  public static void main(String[] args) {
    Animal animal1 = new Dog(); //Java's Animal receives Scala's Dog reference
    Animal animal2 = new Cat(); //Java's Animal receives a Java's Cat reference
  }
}

I get this error about Dog x Animal conversion:

Type mismatch: cannot convert from Dog to Animal

But the same does not happen with the Cat class.

Would someone please explain to me what’s going on here?

  • For some reason I just restarted my computer and it worked. It seems I was having a classpath problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.