Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:33:41+00:00 2026-05-23T09:33:41+00:00

In Scala, I need to override the following, given, Java classes and methods: public

  • 0

In Scala, I need to override the following, given, Java classes and methods:

public abstract class AbstractJava<T> {
    protected abstract T test(Class<? extends T> clazz);
}

public class ConcreteJava extends AbstractJava<Object> {
    @Override
    protected Object test(Class<?> clazz) {
        return null;
    }
}

// Scala
class ConcreteScala extends ConcreteJava {
    protected override def test(clazz: Class[_ <: AnyRef]): AnyRef =
        super.test(clazz)
}

I’m getting the compilation error:

error: ambiguous reference to overloaded definition,

both method test in class ConcreteJava of type 
(clazz: java.lang.Class[_])java.lang.Object

and method test in class AbstractJava of type 
(clazz: java.lang.Class[_ <: java.lang.Object])java.lang.Object

match argument types (Class[_$1]) and expected result type AnyRef

super.test(clazz)

I wouldn’t expect the Scala compiler to refer to an abstract method on a super call. Also, I’d expect it to refer to the direct super class first.

How can I make the Scala class compile?

Thanks!

Edit:

When leaving off the super.test(clazz) call, there’ll be the error message:

error: name clash between defined and inherited member:

method test:(clazz: Class[_ <: AnyRef])AnyRef and
method test:(clazz: java.lang.Class[_])java.lang.Object in class ConcreteJava

have same type after erasure: (clazz: java.lang.Class)java.lang.Object

protected override def test(clazz: Class[_ <: AnyRef]): AnyRef = null

Well, of course these are the same types (or variants) …! – So there’s something wrong with Scala/Java inheritance …

Thanks to michid, there’s a preliminary solution:

class ConcreteScala3 {
  this: ConcreteJava =>
  protected override def test(clazz: Class[_ <: AnyRef]): AnyRef = {
    this.foo() // method of ConcreteJava
    null
  }
}

although we can’t make super calls from here.

Responses are still most welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:33:41+00:00Added an answer on May 23, 2026 at 9:33 am

    There are some limitations when overriding Java methods with raw types. See the corresponding Scala ticket. Specifically Martin Odersky’s comment: “[…] The only thing one can do in these situations is implement a subclass in Java that implements the method. […]”

    However, I pointed out in a blog post earlier that there seems to be a solution for certain cases. The trick is to explicitly declare the self type of the overriding Scala class using an existential type for the raw type on the Java side.

    With this technique I got the following working:

    public abstract class AbstractJava<T> {
        protected abstract T test(Class<T> clazz);
    }
    
    public class ConcreteJava extends AbstractJava<Object> {
        @Override
        protected Object test(Class<Object> clazz) {
            return null;
        }
    }
    
    class ConcreteScala extends ConcreteJava {
        this: AbstractJava[AnyRef] =>
    
        protected override def test(clazz: Class[AnyRef]): AnyRef = {
            super.test(clazz)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to override my parametrized method in scala, I have an abstract class
I need to use java-legacy code with the following method: public void doit(Map <String,
Is there a way in java or scala, that allows to override methods of
I am making a basic graph representation in Scala. abstract class Vertex class Edge
Since I want to generate Scala code from an annotated Scala class, I need
I need a builder library that can be called from Scala and Java. Easy
I need to write a code that compares performance of Java's ArrayList with Scala's
Im quite new in Scala. I need to get the parametrization of a class.
Can Scala be used to script a Java application? I need to load a
My goal is to enhance inside scala code an existing Java class using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.