Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Superclasses and Subclasses

  1. Dec 8, 2004 #1
    Hello all

    I need help in writing a subclass (ManMade) that has 2 constructors and 3 isntance variables. Here is my code so far:

    Code (Text):

    public class Thing
            private double length;        // instance variables (fields)
            private double height;
            private double width;
            private boolean isPurpleFox;
            private double age;
            private static boolean exists;
            public Thing()
                length = 7;
                height = 8;
                isPurpleFox = false;
                age = 0;
                exists = true;
                width = 10;
            public double getlength()
                return length;
            public void setAge(double a)
                age = a;
            public double getage()
                return age;
            public boolean getexists()
                return exists;
            public void setexists(boolean b)
                exists = b;
            public void Triple(int a)
                a = a*3;
            public Thing (double l, double w, double h )
                length = l;
                width =  w;
                height = h;

    // Subclass of Superclass
    // Thing

    [COLOR=Blue]public class ManMade extends Thing {  // inherits from Thing
        private string name;
        private double volume;
        private double surfarea;
        // no argument constructor
        public ManMade()
            super(0, 0); // call the superclass contructor
            name = ;
        // Constructor
        public ManMade( double l, double, w, double h, char s )
            super( l, w, h); // call the superclass constructor
            setName( "Bob" )
            setVolume( v );
            setSurarea( S );[/COLOR]        
    I am not sure whether i am doing this right.

    any help is appreciated
  2. jcsd
  3. Dec 8, 2004 #2
    When you post code, can you please say what you want to do. What is it that you aren't sure of?? You make a subclass by using the extends keyword, like you've done, so what's the problem? All I can see a problem is with the line

    Code (Text):
    name = ;
    You need to write name = " "; cos otherwise the code won't compile (obviously you won't be able to compile that code on its own, as there's no main(), but you know..).
    Last edited: Dec 8, 2004
  4. Dec 10, 2004 #3
    Where is the
    Code (Text):
    constructor with two arguments?
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?