error extending a java class from a scala class

904 views
Skip to first unread message

merlin-hst

unread,
Dec 12, 2010, 3:42:32 PM12/12/10
to Scala IDE User
Hello,

I'm having a (hopefully) simple question. I'd like to extend a java
class from a scala class, but can't get it work ;(

public abstract class AbstractTwitterMessage extends
ExtendedTwitterMessage implements TwitterMessage {
//some java stuff

buildTwitterMessages(...) {
buildTwitterMessagesExtended(...);
}
}

abstract class ExtendedTwitterMessage {

import scala.collection.JavaConversions._
import java.util.{List => JList}

//some scala stuff

def buildTwitterMessagesExtended(messageBuilders:
Array[TwitterMessageBuilder], prefix: String): JList[String] = {
//...
}
}

The interface 'TwitterMessage' is not importend here, the method
'buildTwitterMessagesExtended' is not overidden anywhere. Now I'd like
to use the scala method as shown above. But I always get an error that
"The hierarchy of the type AbstractTwitterMessage is inconsistent"
with no more hints. I've tried it also with a trait but also with no
success.

So what I'm doing wrong here ?

Thank you
Lothar

Scala IDE for Eclipse 1.0.0.201012120008
Eclipse Helios
Java 6 64-bit Windows 7

merlin-hst

unread,
Dec 14, 2010, 3:02:23 PM12/14/10
to Scala IDE User
After hours of searching it seems to me that it's not possible to
extend java classes/interfaces from scala classes/traits - at least
for me ;( The other way works perfect. I changed my code a little bit
so now it's looks like this:


public interface TwitterMessage {
//some stuff...
}


trait ExtendedTwitterMessage extends TwitterMessage {
// ... (extends a java interface)

import scala.collection.JavaConversions._
import java.util.{List => JList}

def buildTwitterMessagesExtended(messageBuilders:
Array[TwitterMessageBuilder], prefix: String): JList[String] = {
//some stuff...
}
}


public abstract class AbstractTwitterMessage implements
ExtendedTwitterMessage {
//... (implements a scala trait which extends a java interface)

buildTwitterMessages(...) {
buildTwitterMessagesExtended(...);
}
}


public class TwitterDailyMessage extends AbstractTwitterMessage {
//... (extends an abstract java class which implements a trait
which extends a java interface)
}

Now I'm getting the following error:
The type TwitterDailyMessage must implement the inherited abstract
method ExtendedTwitterMessage.buildTwitterMessagesExtended...

What's that ? Do I miss something ? Of course I'd like to use the
method from trait. Or do I have to do something more to be able to
extend java from scala ? I've also tried it with Galileo but with the
same error.

It would be really helpfull to me if someone could give me a hint.

Lothar

David Bernard

unread,
Dec 14, 2010, 3:19:12 PM12/14/10
to scala-i...@googlegroups.com
hi,

I'm not a java/scala mixe user.
Trait with implementation are not interface, trait without any implementation is like a Java interface

IMHO,

  public abstract class AbstractTwitterMessage implements ExtendedTwitterMessage  {

will not keep implementation from ExtendedTwitterMessage
try :

  public abstract class AbstractTwitterMessage extends ExtendedTwitterMessage  {


It's doable to extends Java class/interfaces from scala (more doable and did than the reverse).
May be a full, simple example will help you and us to work together.

/davidB

Kevin Wright

unread,
Dec 14, 2010, 3:46:11 PM12/14/10
to scala-i...@googlegroups.com
On 14 December 2010 20:19, David Bernard <david.be...@gmail.com> wrote:
hi,

I'm not a java/scala mixe user.
Trait with implementation are not interface, trait without any implementation is like a Java interface

*All* traits are interfaces.  For some trait Foo:

trait Foo {
  def doIt = println("xxx")
}

You'll end up with the equivalent Java

interface Foo {
  public void doIt()
}

class Foo$class {
  public static void doIt(self: Foo) { System.out.println("xxx"); }
}

So all the concrete methods become static methods on <trait>$class, and the actual instance is prepended to the argument list of all such methods.


--
Kevin Wright

gtalk / msn : kev.lee...@gmail.com
mail: kevin....@scalatechnology.com
pulse / skype: kev.lee.wright
twitter: @thecoda

merlin-hst

unread,
Dec 14, 2010, 3:54:37 PM12/14/10
to Scala IDE User
For now I'm just trying to explore the possibilities to use
implementations from scala traits within java classes. So it's a
little bit like "playing around" ;)

I've found an interesting description here (even it doesn't seems to
work) :

http://www.codecommit.com/blog/java/interop-between-java-and-scala

If I understand it right I have to delegate to a class
'ExtendedTwitterMessage$class'. The class file itself is still there
but I can't call it by myself (eclipse doesn't find it).. So because
of the big differences it may be not really usefull to use the trait
implementation in java.

Lothar

merlin-hst

unread,
Dec 14, 2010, 4:04:05 PM12/14/10
to Scala IDE User
What I've forgotten to mention is that all scala packages and classes
in the "Navigator" view and also in the "Project Explorer" view are
marked with a '?' - sign. But there are no compiler errors in the
scala code (but of course in the java code). What does it mean ? It's
a litte bit confusing.

Lothar

merlin-hst

unread,
Dec 14, 2010, 4:10:31 PM12/14/10
to Scala IDE User
Sorry my fault - the files were not under version control ;)

Lothar
Reply all
Reply to author
Forward
0 new messages