title: Problem in upcasting in Java?
body: <p>Could someone please explain why this happening:</p>
  
  <pre><code>class Apple {
  
     String type;
  
     setType(){
        System.out.println("inside apple class");
        this.type = "apple";
     }
  
  }
  
  class RedApple extends Apple {
  
      String type;
  
      setType() {
        System.out.println("inside red-apple class");
         this.type = "redapple";
      }
  }
  
  int main {
  
      RedApple red = new RedApple();
      Apple apple = (Apple) red;
      apple.setType();
  
  }
  </code></pre>
  
  <p>But the output produced is: </p>
  
  <pre><code>"inside red-apple class”
  </code></pre>
  
  <p>Why does the <code>.setType()</code> method execute the sub-class method, and not the super-class method, even though I am up-casting, as can be seen?</p>