2 Replies Latest reply: Feb 3, 2007 5:15 AM by 800322 RSS

    Constructors and Subclasses

    796381
      Ok, this code may look a little weird, I was just playing around with using subclasses and super classes. Here is the code. Now when I compile it wants me to add a Thing() constructor to the Thing class and i'm not sure why. Everything works fine when I do, I'd just like to know why I need to.

      ThingDemo.java
      public class ThingDemo
      {
           public static void main(String args[])
           {
                Thing myThing = new Thing(1, 2);
                myThing.things();
                SpecialThing mySpecialThing = myThing.getSpecialThing(3);
                mySpecialThing.things();
           }
      }
      Thing.java
      public class Thing
      {
           public int thingInt;
           public int thingInt2;
           
           public Thing(int i, int j)
           {
                this.thingInt = i;
                this.thingInt2 = j;
           }
           
           public SpecialThing getSpecialThing(int i)
           {
                return new SpecialThing(this, i);
           }
           
           public void things()
           {
                System.out.println("thingInt = " + thingInt);
                System.out.println("thingInt2 = " + thingInt2);
           }
      }
      SpecialThing.java
      public class SpecialThing extends Thing
      {
           public int thingInt3;
           
           public SpecialThing(Thing t, int i)
           {
                this.thingInt = t.thingInt;
                this.thingInt2 = t.thingInt2;
                this.thingInt3 = i;
           }
           
           public void things()
           {
                super.things();
                System.out.println("thingInt3 = " + thingInt3);
           }
      }
      I would appreaciate it if anyone could explain why this is happening or redirect me to a page that does.

      Thanks
      Krimson Eagl

      Message was edited by:
      KrimsonEagl