Polymorphism

Polymorphism is one of the object oriented concepts. Polymorphism means allowing objects with differing behavior that can be substitutable for one another.

You can take advantage of polymorphism by using Inheritance, either just Interface Inheritance or with Implementation Inheritance .

Method Overriding facilitates the differing behaviors in subtypes.

Without polymorphism …


if (...file...) {
    readFromFileStream(new FileInputStream("filename"));
} else if (...URL...) {
    readFromURLStream(new  URL(url).openStream());
} else if (...console...) {
    readFromConsoleStream(System.in);
} else if ...

  • Notice that the if else statements does similar things – to read from an input stream.

With polymorphism…

</pre>
<pre class="prettyprint"><span class="kwd">void</span><span class="pln"> readFromStream</span><span class="pun">(</span><b><span class="typ">InputStream</span><span class="pln"> stream</span></b><span class="pun">)</span> <span class="pun">{</span>
    <span class="pun">...</span><span class="pln">
    stream</span><span class="pun">.</span><b><span class="pln">read</span><span class="pun">()</span></b><span class="pun">;</span>
    <span class="pun">...</span>
<span class="pun">}</span></pre>
<pre>

InputStream is an abstract class, so any object of InputStream (extends InputStream) can be passed in the readFromStream(InputStream stream) regardless of the implementation of the read() function.

For example,

</pre>
<pre class="prettyprint"><span class="pln">readFromStream</span><span class="pun">(</span><span class="kwd">new</span> <span class="typ">FileInputStream</span><span class="pun">(</span><span class="str">"filename"</span><span class="pun">));</span></pre>
<pre class="prettyprint"><span class="pln">readFromStream</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> URL</span><span class="pun">(</span><span class="str">"http://google.com"</span><span class="pun">).</span><span class="pln">openStream</span><span class="pun">());</span></pre>
<pre class="prettyprint"><span class="pln">readFromStream</span><span class="pun">(</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">in</span><span class="pun">);</span></pre>
<pre>

FileInputStream extends InputStream, so it is legal to pass FileInputStream object to readFromStream() method.

In general, any object that extends InputStream can be passed to the readFromStream() method.

The type of the object you passed determines the what implementation of the behavior to be executed.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s