title: Java: JSONObject inheritance
body: <p>I'm having trouble converting objects into JSONObject (org.json.JSONObject) in java.</p>
  
  <p>I have an object X that is a child object Y. When I make "new JSONObject (this).ToString()", only the attributes of the object X (child) that are created in the JSONObject.</p>
  
  <p>See an example below:</p>
  
  <p>Parent Class:</p>
  
  <pre><code>import org.json.JSONObject; 
  
  public class Class1 {
  private String name;
  private int number;
  
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public int getNumber() {
      return number;
  }
  public void setNumber(int number) {
      this.number = number;
  }
  public String toString(){
      return new JSONObject(this).toString(); 
  }
  }
  </code></pre>
  
  <p>Child class:</p>
  
  <pre><code>import org.json.JSONObject;
  public class Class2 extends Class1 {
  
  private String attrChildString;
  private int attrChildInt;
  /**
   * @param args
   */
  public static void main(String[] args) {
      Class2 class2 = new Class2();
      class2.setName("test");
      class2.setNumber(5);
      class2.setAttrChildInt(10);
      class2.setAttrChildString("child");
  
      System.out.println("toString child : " + class2.toString());
      System.out.println("toString Parent : " + class2.toStringParent());
  }
  public String getAttrChildString() {
      return attrChildString;
  }
  public void setAttrChildString(String attrChildString) {
      this.attrChildString = attrChildString;
  }
  public int getAttrChildInt() {
      return attrChildInt;
  }
  public void setAttrChildInt(int attrChildInt) {
      this.attrChildInt = attrChildInt;
  }
  
  public String toString(){
      return new JSONObject(this).toString(); 
  }
  
  public String toStringParent(){
      return super.toString();
  } 
  }
  </code></pre>
  
  <p><strong>Result:</strong> toString child : {"attrChildInt":10,"attrChildString":"child"}
              toString Parent : {"attrChildInt":10,"attrChildString":"child"}</p>
  
  <p>But I need to display the values ​​of the parent.
  <strong>Example:</strong>  {"attrChildInt":10,"attrChildString":"child","name":"test","number":"5"}</p>
  
  <p>Any Idea?</p>
  
  <p>Thanks.</p>