Consider Enums Instead of Constants

Using constants can be error prone. You always need to validate that the value being passed is valid.

<pre class="prettyprint"><span class="kwd">class</span> <span class="typ">Suit</span> <span class="pun">{</span>        
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> CLUBS </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">;</span>
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> SPADES </span><span class="pun">=</span> <span class="lit">2</span><span class="pun">;</span>
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> HEARTS </span><span class="pun">=</span> <span class="lit">3</span><span class="pun">;</span>
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> DIAMONDS </span><span class="pun">=</span> <span class="lit">4</span><span class="pun">;</span>
<span class="pun">}</span></pre>
<pre class="prettyprint"><span class="kwd">void</span><span class="pln"> method</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> suit</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">if</span> <span class="pun">(</span><span class="pln">suit </span><span class="pun"><</span> <span class="lit">1</span> <span class="pun">||</span><span class="pln"> suit </span><span class="pun">></span> <span class="lit">4</span><span class="pun">)</span> <span class="pun">{</span>
        <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">IllegalArgumentException</span><span class="pun">(</span><span class="str">"Invalid suit value: "</span> <span class="pun">+</span><span class="pln"> suit</span><span class="pun">);</span>
    <span class="pun">}</span>
    <span class="pun">...</span>
<span class="pun">}</span></pre>

What happens if we add a suit? What happens if we remove a suit? All the client code needs to be updated!

</pre>
<pre class="prettyprint"><span class="kwd">enum</span> <span class="typ">Suit</span> <span class="pun">{</span><span class="pln">     
    CLUBS</span><span class="pun">,</span><span class="pln"> SPADES</span><span class="pun">,</span><span class="pln"> HEARTS</span><span class="pun">,</span><span class="pln"> DIAMONDS
</span><span class="pun">}</span></pre>
<pre class="prettyprint"><span class="kwd">void</span><span class="pln"> method</span><span class="pun">(</span><span class="typ">Suit</span><span class="pln"> suit</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="typ">Validate</span><span class="pun">.</span><span class="pln">notNull</span><span class="pun">(</span><span class="pln">suit</span><span class="pun">);</span>
    <span class="pun">...</span>
<span class="pun">}</span></pre>
<pre>

Client code:

</pre>
<pre class="prettyprint"><span class="pln">method</span><span class="pun">(</span><span class="typ">Suit</span><span class="pun">.</span><span class="pln">CLUBS</span><span class="pun">);</span></pre>
<pre class="prettyprint"><span class="kwd">import</span> <span class="kwd">static</span> <span class="pun">....</span><span class="typ">Suit</span><span class="pun">.*;</span>
    <span class="pun">...</span><span class="pln">
    method</span><span class="pun">(</span><span class="pln">CLUBS</span><span class="pun">);</span>
    <span class="pun">...</span></pre>
<pre>

Enums can also define their own instance variables and methods, so you can remove the if-else blocks from the clients and instead push specific behaviors or values to the enums.

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(&amp;amp;amp;amp;amp;quot;filename&amp;amp;amp;amp;amp;quot;));
} 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.

 

Inheritance

In this blog post, I’ll discuss about one of the concepts of Object Oriented Programming which is Inheritance. 

Inheritance is a way of deriving a class from another class. In the other words, a class can inherit the fields and methods from other class.

Code reuse

Inheritance is one way of achieving code reuse. Here’s why, when you want to create a new class and some functionality in it has already in another class, you can derive your class to the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write the code again in your new class.

Subclass is the class that is derived from another class (also called extended class, derived class, or child class). This is more specific type of a class.

Superclass is the class from which a subclass is derived (also called parent class or base class).

inheritanceIn the picture above, Person class is the superclass of Student class and Employee class. On the other hand, Student class and Employee class are the subclasses of Person class. Student and Employee is a more specific type of a Person.

inheritance

Look at the class diagram above, notice that the Student class and Employee has no basic fields like firstName, lastName, lastName and age. It is because those fields are already inherited from the Person class. However, you can also declare those fields again with the same name in the subclass, thus, you are hiding the fields in the superclass (it can cause confusion and it is not recommended).

Not only the fields are inherited but also the methods (except for private methods) of the Person class. The methods eat() and sleep() of Person is inherited in the Student and Employee class.

You can add fields and methods to the subclass. In the case above, Student has a course and yearLevel fields and a study() method and Employee has salary and designation fields and a raiseSalary(int percent) method. By adding those fields and methods, you are extending the functionality of the class and that what makes the subclass more specific type.

You can change the behavior of the superclass by overriding it. Override a method by define a method with the same method signature, a method signature is the method name and the number and type of its parameters. Return types are not considered to be a part of the method signature.

If you liked this post, don’t forget to subscribe to this blog for more updates.