title: Cast to an abstract class...how this is possible?
body: <p>I am actually reading a book about design patterns in java and I am a newbie :)</p>
  <p><a href="https://rads.stackoverflow.com/amzn/click/com/0321333020" rel="nofollow noreferrer" rel="nofollow noreferrer">http://www.amazon.com/Design-Patterns-Java-TM-Software/dp/0321333020/</a> at the chapter about composite pattern I came across a code that puzzles me, a cast to an abstract class, I have also not well understood what happens when the sub-clase calls the constructor of the abstract superclass, can you help me please!</p>
  <p>The cast that I am talking about is in the isTree(Set visited)</p>
  <pre><code>        MachineComponent c = (MachineComponent) i.next();
          if (visited.contains(c) || !c.isTree(visited)) 
  
        
  </code></pre>
  <p>How can we call the <code>isTree</code> method of the subclass after a cast to his abstract superclass while the <code>isTree</code> superclass method is abstract?</p>
  <p>Here are snippets of the two classes:</p>
  <pre><code>package com.oozinoz.machine;
  /*
   * Copyright (c) 2001, 2005. Steven J. Metsker.
   */
  
  import java.util.*;
  import com.oozinoz.iterator.ComponentIterator;
  
  /**
   * Objects of this class represent either individual machines or composites of
   * machines.
   */
  
  public abstract class MachineComponent {
  
      /*
       * Subclasses implement this to support the isTree() algorithm.
       */
  
      protected abstract boolean isTree(Set s);
   
      // rest of class omitted
  }
  </code></pre>
  <p>2:</p>
  <pre><code>package com.oozinoz.machine;    
  /*
   * Copyright (c) 2001, 2005. Steven J. Metsker.
   */
  
  import java.util.*;
  import com.oozinoz.iterator.ComponentIterator;
  import com.oozinoz.iterator.CompositeIterator;
  
   /**
   * Represent a collection of machines: a manufacturing line, a bay, or a
   * factory.
   */
  
  public class MachineComposite extends MachineComponent {
      protected List components = new ArrayList();
  
      /**
       * @param visited a set of visited nodes
       * @return true if this composite is a tree
       * @see MachineComponent#isTree()
       */
  
      protected boolean isTree(Set visited) {
          visited.add(this);
          Iterator i = components.iterator();
          while (i.hasNext()) {
              MachineComponent c = (MachineComponent) i.next();
              if (visited.contains(c) || !c.isTree(visited)) 
                  return false;
          }
          return true;
      }
  
      // rest of class omitted
  }
  </code></pre>