title: Regarding Superclass variable referencing subclass object in Java
body: <p>Java Inheritance: Question regarding the parent reference variable.</p>
  
  <p>I am learning Java and going through the inheritance problems in Java, have one question here.</p>
  
  <pre><code>public class Shape {
      private String shapeName;
      private String area;
      //normal getters and setters
  }
  
  public class Triangle extends Shape {
      private String typeOfTriangle;
      //normal getter and setter.
  }
  
  public class ShaperRunner {
      public static void main(String[] args) {
          Shape shape = new Triangle();
          // Here the shape reference variable referencing to Triangle object is allowed, however using this shape object. I cannot call
          shape.setTypeOfTriangle("Scalence")
          System.out.println("The shape of the triangle is " + shape.getTypeOfTriangle());
      }
  }
  </code></pre>
  
  <p>My Question is if shape reference variable can be used to call only the getters and setters confined to what is defined in it, then why do we even consider using shape object to reference Triangle object. I have seen such code in few places but could not understand what is the advantange of doing it.</p>