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

  • SEARCH
  • Home
  • 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 6056265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:21:26+00:00 2026-05-23T08:21:26+00:00

I need to override the following Java method in a Scala class: public class

  • 0

I need to override the following Java method in a Scala class:

public class Test<T> {
    public T test(T o, boolean x) {
        if (x) {
            return o;
        }
        return null;
    }
}

With the following approach (in Scala), the compiler complains, “Expression of type Null doesn’t conform to expected type T”:

class Test2[T] extends Test[T] {
  override def test(o: T, x: Boolean): T = {
    if (x) {
      return o
    }
    return null
  }
}

I’ve also tried to define Option[T] as return value, but then again, the compiler complains that the method signatures wouldn’t match.

Any idea? – Thanks!


Edit:

Daniel’s suggestion works for the problem as originally posted; however, my actual problem unfortunately still differs slightly (by having the generic type parameter in the method, not class, definition) (sorry):

Java:

public class Test {
    public <T> T test(T o, boolean x) {
        if (x) {
            return o;
        }
        return null;
    }
}

Scala:

class Test2 extends Test {
  override def test[T >: Null](o: T, x: Boolean): T = {
    if (x) {
      return o
    }
    return null
  }
}

Compilation again fails with the error, “Expression of type Null doesn’t conform to expected type T”.
(I believe that’s because the override does not cover any possibilities – i.e., something of type Nothing could be passed to Test.test(..) – well, could it? 😉 )

What does work is throwing a RuntimeException instead of returning null as Ricky suggested; nonetheless, I’d be grateful for further input.

Thanks all!

  • 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-23T08:21:26+00:00Added an answer on May 23, 2026 at 8:21 am

    You need this:

    class Test2[T >: Null] extends Test[T] {
    

    The problem is that Nothing cannot be null, and, being the subtype of everything, it is a valid value of T. So you need to specify Null as the lower bound.

    EDIT

    Unfortunately, there’s no good way around your actual problem. In this case, you’ll have to write null.asInstanceOf[T] and leave it at that.

    And, yes, you can call it with non-nullable types. Try this, for example:

    object Test3 {
      val test = new Test();
      val x: Int = test.test(5, false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How come java doesn't allow the following generic return type: public <T extends Enum<T>
We need to test a java build with the languages set to different values.
My goal is to enhance inside scala code an existing Java class using a
How do you do the following i MonoDroid? public class ItemListAdapter : BaseAdapter {
I have an Android activity called main.java and BPMClass.java class. I need to call
I need to register a receiver. I have been using the following pattern: @Override
Need to locate the following pattern: The letter I followed by a space then
Need some help about with Memcache. I have created a class and want to
In Effective Java, the author states that: If a class implements Cloneable, Object's clone
I tried to override the dynamic method addTo* provided by Grails/GORM but it doesn't

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.