2 Replies Latest reply on Jun 22, 2010 8:17 AM by 807580

    How can I casting from parent class to children class

    807580
      Dear,
      Could someone help me to casting from parent class to children class.
      I have class like this

      class parent{
      String name;
      String id;
      public String getId() {
      return id;
      }
      public void setId(String id) {
      this.id = id;
      }
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }
      }

      class children extends parent{
      String address;

      public String getAddress() {
      return address;
      }
      public void setAddress(String address) {
      this.address = address;
      }
      public children() {
      }
      public children(parent p) {
      //Do init super class here
      }
      }
      //--------------------------------
      In the constructor
      public children(parent p) {
      //Do init super class here
      }

      I like to init super class by object p (this is instance of parent class). The way to do is using:
      public children(parent p) {
      super.setId(p.getId());
      super.setName(p.getName());
      }
      But I don't like this, because, for example I have parent class with over 30 proberties, it take time to do like that.
      There are any way to use super operation to init parent class, for example super = p;

      Could you show me the way.
      Thanks alot
        • 1. Re: How can I casting from parent class to children class
          800330
          If I understand your question correctly, you are in need of a copy constructor for you class Parent. A copy constructor behaves like this:
             Parent one = new Parent();
             one.setName("..."); 
             //... and all other properties of interest
             Parent two = new Parent(one);
             //Now two != one, but one.getName().equals(two.getName) for property name and all others
          The copy constructor is programmed in the Parent class, more later. Then for your child class you can use it as follows
             public class Children extends Parent {
                 public Children(Parent p) {
                    super(p);
                 }
             }
          There are at least 3 ways of programming a copy constructor:
          1. Just bite the bullet: type the assignment for each field this.name = p.getName()
          2. Use reflection to find all common setters/getters dynamically and assign using them
          3. Use a code generator that uses 2 to give you the code for solution 1 for you to paste in.

          If you find doing this a lot, there are frameworks that can do these mappings, like Dozer

          (PS be carefull with Date fields, don't copy the reference but create a new and equals instance, the dirty way would be this.birthdate = new Date(p.getBirthdate.getTime()); )
          • 2. Re: How can I casting from parent class to children class
            807580
            That is what I want to finding
            Thanks alot