Should I remove the nullability of overriden methods that are not annotated with nullable in an inherited class

kotlin
kotlin lateinit
kotlin inheritance example
kotlin inherit multiple classes
kotlin override function
kotlin companion object
google kotlin best practices
kotlin inheritance data class
Android 3.5.1

I was using the WebView and I noticed that when I override some of the methods all the parameters are nullable types:

webview.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
         return super.shouldOverrideUrlLoading(view, request)
    }
}

Which means I have to use the safe call operator to use them. However, when I looked at the WebViewClient class that I have overridden the method from they are not specified as nullable annotation in the Java code.

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    return shouldOverrideUrlLoading(view, request.getUrl().toString());
}

So I am left thinking do I remove the nullability from the overridden method or keep them?

The source of this issue comes from Interoperability between Java and Kotlin. There are some basic language level differences between Java and Kotlin which causes interoperability issues. Android Studio provides some Lint checks to warn them, such as Unknown Nullness. (reference)

By taking a look at details of Unknown nullness Lint check from android.com, we see that:

To improve referencing code from Kotlin, consider adding explicit nullness information here with either @NonNull or @Nullable.

and on developer.android.com:

If you use Kotlin to reference an unannotated name member that is defined in a Java class (e.g. a String), the compiler doesn't know whether the String maps to a String or a String? in Kotlin. This ambiguity is represented via a platform type, String!.

and on kotlinlang.org:

Any reference in Java may be null, which makes Kotlin's requirements of strict null-safety impractical for objects coming from Java. Types of Java declarations are treated specially in Kotlin and called platform types.

Therefore, when we override a Java method that its arguments are not annotated with nullity annotations, the IDE adds nullable sign (?) for arguments in Kotlin class. It leads to avoid throwing NullPointerException when the method is called in Java by passing a null value for one of the arguments.

webview.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(
        view: WebView, // <- potential to throw NPE before executing the function block!
        request: WebResourceRequest // <- as well!
    ): Boolean {
        return super.shouldOverrideUrlLoading(view, request)
    }
}

In a nutshell, we SHOULD NOT remove ? sign from function arguments, when the overridden method is defined in a Java class.

Use common Kotlin patterns with Android, The first is that it reduces the likelihood that class names will collide with each other What can you do to clear a background color from a Swing user interface B. Call setBackground() with no arguments. C. Call setBackground(null). 2. What annotation indicates that a method overrides the same method in a Inherited 3. Should I remove the nullability of overriden methods that are not annotated with nullable in an inherited class ; java type promotion in parameters ; Why can java not infer a supertype ; Java IntStream iterate vs generate when to use what?

Unlike Kotlin , Java objects by default can accept null values

@Nullable annotation is just used for operations like code analysers (for eg. if the @Nullable parameter is not handled inside the method then it will show warning)

@NonNull annotation is used to specify that the value received can't/won't be null

if(@NonNull){
      can omit ? check
}else if(@Nullable){
     Mandatory to put ? check
}else(No annotation){
     Not mandatory but put on safer side .
     Passing null from Java into Kotlin fun without ? will lead to NPE
    if(putting ? check){
     java equivalent Kotlin param  (@Nullable Webview view)
    } else{
     java equivalent Kotlin param (@NonNull Webview view)
    }

}

Also Refer this : https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#null-safety

Sams Teach Yourself Java in 21 Days (Covering Java 12), Barnes & , It should be clear now that null annotations add more information to your Java provenance it may not be possible to add null annotations to all classes at once. null annotations as if annotations from an overridden method were inherited. Should I remove the nullability of overriden methods that are not annotated with nullable in an inherited class ; java type promotion in parameters ; Why can java not infer a supertype ; Java IntStream iterate vs generate when to use what?

Using null annotations, An assignment x.f = v where v could be null and field f is not annotated with @​Nullable. class C {. String f;. C () { // field f not initialized and not annotated @​Nullable. } where v can be null and the corresponding parameter in method m is not annotated If the annotations are correct, you can remove the redundant case. Should I preemptively make it [MaybeNullWhen(false)] out string instead, just in case someone wants to pass it to some generic API from another library with an out T parameter? What's driving the signature is no longer anything intrinsic to the method. Now it's constrained by how it might be used.

They are not specified as nullable annotation in the Java code.

If that's true note that you risk throwing a NullPointerException if not specified as nullable annotation in the Java code and assign a null value. so remove the nullability from the overridden method if not specified as nullable annotation in the Java code.

For more detail read this also this

Eradicate warnings, You can also use as to cast to a nullable type. The difference between this and the previous as? cast is that this one will fail if x is a non-null instance of another  @Nullable and @NotNull @Nullable and @NotNull annotations let you check nullability of a variable, parameter, or return value. They help you control contracts throughout method hierarchies, and if IntelliJ IDEA spots that the contract is being violated, it will report the detected problem, and will point to the code where NullPointerException may occur.

On the language-level, this can be generalized:

For proper Java interoperability, the Kotlin code should reflect the annotations of the Java code.

The linter only complains about lacking annotations in the other direction, for Kotlin interoperability.

See this recent article on How to write Java friendly Kotlin code?

Inheritance, Note that, if the Java class only has a setter, it will not be visible as a property in If a Java library uses a Kotlin keyword for a method, you can still call the If we choose a non-null type, the compiler will emit an assertion upon assignment. Note: the migration status of a nullability annotation is not inherited by its type  The nullable annotation context and nullable warning context can be set for a project using the Nullable element in your .csproj file. This element configures how the compiler interprets the nullability of types and what warnings are generated.

Calling Java from Kotlin, @Nullable and @NotNull annotations let you check nullability of a variable, then call this method with a parameter that potentially can be null, IntelliJ IDEA will The @Nullable annotation of the parameter in the parent method requires the @Nullable annotation in the child class method A method should not return null. Should I remove the nullability of overriden methods that are not annotated with nullable in an inherited class Invalid SplitApkBundle. The bundle targets unknown languages:[gr]. google play console

@Nullable and @NotNull - Help, dereferencing variables (fields, local variables, parameters) that can be null. The @NotNull Methods with the @Nullable annotation in the parent method can have either. @Nullable or @NotNull annotations in the child class method. @​Nullable-annotated methods should not override ones annotated as @NotNull. I think this is a problem (don't want to say it is bug, it might be intentional) of the inspection called "@NotNull/@Nullable problems". You can simply disable the rule "Report non-annotated parameters or methods overriding @NotNull" in Settings / Inspections / @NotNull/@Nullable problems

[PDF] Nullable How-To, Overriding default methods; 2.8. Inheritance of state gotchas; 2.17. Classes do not need to have the same base name as their source file definitions null as the vararg parameter, then the argument will be null and not an array of length will be removed but any new parameters found within existing annotations will be​  Video. It seems they should be marked as Inherited = false, because that's what we did for the other annotations.But it seems the behavior of this is odd when the method it's applied to is overriden because the inherited method doesn't need to call the base.