Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>extends ArrayAdapter&lt;Event&gt;</code></p> <p>The type restriction here will influence on the return types of methods in the class, and the argument types of methods.</p> <p>Here is an example, if you have a class:</p> <pre><code>class SomeClass&lt;T&gt; { protected T value; public void setValue (T value) { this.value = value; } public T getValue () { return value; } } </code></pre> <p>And if you have another class:</p> <pre><code>class SubClass extends SomeClass { @Override public void setValue (Event value) { // Fail! It is not overriding the super class' method. this.value = value; // Warning! Unchecked types (maybe inconsistent). } } </code></pre> <p>If you remove the <code>@Override</code> annotation, it will run. But the <code>extends SomeClass</code> is useless and might cause problem if you keep it there -- there will be two very similar methods: <code>setValue(Event)</code> and <code>super.setValue(T)</code>. Now the question is <strong>will the subclass have access to the <code>super.setValue(T)</code> method?</strong> I will explain it in the end, see "A missing type parameter bounding example".</p> <p>So, you need to specify the type in declaration:</p> <pre><code>class SubClass extends SomeClass&lt;Event&gt; { @Override public void setValue (Event value) { // Correct now! this.value = value; } } </code></pre> <p>Also, if you declare an inconsistent type:</p> <pre><code>class SubClass extends SomeClass&lt;String&gt; { @Override public void setValue (Event value) { // Fail! Not overriding. this.value = value; // Fail! Inconsistent types. } } </code></pre> <p>So the type restricts the behavior of class body.</p> <hr> <hr> <h2>A missing type parameter bounding example:</h2> <hr> <pre><code>import java.lang.reflect.*; class Super&lt;T&gt; { public void method (T t) { System.out.println("Hello"); } public void method2 () { } } public class Test extends Super { /*public void method (Object t) { System.out.println("world"); }*/ /*public &lt;T&gt; void method (T t) { }*/ public static void main (String args[]) { new Test().method(""); for (Method m : Test.class.getMethods()) { System.out.println(m.toGenericString()); } } } </code></pre> <ul> <li><p>If I comment <code>method()</code> in the subclass, it is compiled with a warning: <code>Test.java uses unchecked or unsafe opertations</code>. In the running result, it turned the generic type <code>T</code> into <code>Object</code>: <code>public void Test.method(java.lang.Object)</code>.</p></li> <li><p>If I only uncomment the first <code>method()</code> in the subclass, it is compiled with no warnings. In the running result, the subclass owns one <code>public void Test.method(java.lang.Object)</code>. But it doesn't allow <code>@Override</code> annotation.</p></li> <li><p>If I only uncomment the second <code>method()</code> in the subclass (which also has a generic type bounding), the compile fails with an error: <code>name clash</code>. It also doesn't allow <code>@Override</code> annotation. If you do so, it throws a different error: <code>method does not override</code>. </p></li> <li><p><code>method2()</code> is inherited by the subclass unanimously. But you also can't write the following code:</p> <p>in superclass: <code>public void method2 (Object obj)</code> and in subclass: <code>public &lt;T&gt; void method2 (T obj)</code>. They are also ambiguous and is not allowed by the compiler.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload