Commit 79ef7fd0 authored by Dr. Martin Goik's avatar Dr. Martin Goik

overloading matching exercise

parent fec186d9
......@@ -1808,6 +1808,126 @@ void main(void) {
</mediaobject>
</figure>
<qandaset defaultlabel="qanda" xml:id="sd1_qanda_overloadMatching">
<title>Will a match be found?</title>
<qandadiv>
<qandaentry>
<question>
<para>We reconsider <xref
linkend="sd1_fig_overloadingPrinciple"/> adding the following
statements:</para>
<programlisting language="none">Print p = new Print();
final long value = 44L;
p.<emphasis role="red">print(value)</emphasis>;</programlisting>
<para>Answer the following questions:</para>
<orderedlist>
<listitem>
<para>Does the <classname>Print</classname> class provide a
method matching exactly?</para>
</listitem>
<listitem>
<para>Read <link
xlink:href="https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.12.2.1">Identify
Potentially Applicable Methods</link> and determine the set
of potentially matching methods with respect to
<code>p.<emphasis
role="red">print(44L)</emphasis></code>.</para>
</listitem>
<listitem>
<para>Test your result from the previous step by actually
executing the given sample code.</para>
</listitem>
</orderedlist>
</question>
<answer>
<orderedlist>
<listitem>
<para>The <classname>Print</classname> class does not
provide an exactly matching method. The method signature
<code>print(long)</code> is absent:</para>
<programlisting language="java">// Not being defined in class Print
public void print(long l) {
System.out.println("long value " + l);
}</programlisting>
</listitem>
<listitem>
<para>We create the set of potentially matching methods by
exclusion:</para>
<orderedlist>
<listitem>
<para><code language="java">public void print()</code>
does not accept any argument at all and thus does not
match.</para>
</listitem>
<listitem>
<para><code language="java">public void print(int
i)</code> accepts a single argument of type <code
language="java">int</code>. But we cannot assign an
eight byte <code language="java">long</code> to a four
byte <code language="java">int</code>:</para>
<programlisting language="java">final long value = 44L;
int a = value; //Incompatible types. Required: int Found: long</programlisting>
<para>Therefore <code language="java">public void
print(int i)</code> does not match either.</para>
</listitem>
<listitem>
<para><code language="java">public void print()</code>
and <code language="java">public void print(int i, int
j)</code> expect zero or two arguments respectively
rather then one being provided by <code>p.<emphasis
role="red">print(44L)</emphasis></code> and thus do not
match.</para>
</listitem>
<listitem>
<para>We may assign a <code language="java">long</code>
to a <code language="java">double</code> by virtue of
widening:</para>
<programlisting language="java">final long value = 44L;
final double d = value; // widening, o.K.</programlisting>
<para>Thus <code language="java">public void
print(double d)</code> potentially matches.</para>
</listitem>
</orderedlist>
<para>Thus <code language="java">public void print(double
d)</code> is the only matching candidate.</para>
</listitem>
<listitem>
<para>Execution yields:</para>
<screen language="java">double value 44.0</screen>
<para>Thus <code language="java">public void print(double
d)</code> indeed is being chosen by the overloading
mechanism.</para>
</listitem>
</orderedlist>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<figure xml:id="sd1_fig_sysoutPrintlnOverload">
<title>Example: System.out.print(...)</title>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment