Constructing Objects

My Blueprint for creating House Objects
| SmallHouse Class (subclass) | House Class (superclass) | LargeHouse Class (subclass) |
| /** A small house has a defined number of bedrooms (2) and bathrooms (1), a houseId that counts up depending on the number of house objects and also doesn't have a garage.*/ public class SmallHouse extends House { /** Constructs a house with a bedroomNumber of 2, a bathroonNumber of 1 and no garage. */ public SmallHouse() { super.setBedroomNumber(2); super.setBathroomNumber(1); super.setGarage(false); } } |
/** A house has a defined number of bedrooms and bathrooms, a houseId that counts up depending on the number of house objects and may or may not have a garage.*/ public class House { private int bedroomNumber; private int bathroomNumber; private boolean garage; private static int houseId = 1000; /** * Constructs a house with a houseId of 1001. */ public House() { houseId++; } /** * Sets the number of bedrooms. * @param bed the number of bedrooms. */ public void setBedroomNumber(int bed) { bedroomNumber = bed; } /** * Sets the number of bathrooms. * @param bath the number of bathrooms. */ public void setBathroomNumber(int bath) { bathroomNumber = bath; } /** * Sets the garage instance variable to true or false. * @param g the house has a garage (either true or false) . */ public void setGarage(boolean g) { garage = g; } /** * Gets the number of bedrooms. * @param bedroomNumber the number of bedrooms. */ public int getBedroomNumber() { return bedroomNumber; } /** * Gets the number of bathrooms. * @param bathroomNumber the number of bathrooms. */ public int getBathroomNumber() { return bathroomNumber; } /** * Gets the value of garage(either true or false). * @param garage value of garage(either true or false). */ public boolean getGarage() { return garage; } /** * Gets the house Id Number. * @param houseId the ID number of the house object. */ public static int getHouseId() { return houseId; } } |
/** A large house has a defined number of bedrooms (4) and bathrooms (2), a houseId that counts up depending on the number of house objects and also has a garage.*/ public class LargeHouse extends House { /** Constructs a house with a bedroomNumber of 4, a bathroomNumber of 2 and a garage. */ public LargeHouse() { super.setBedroomNumber(4); super.setBathroomNumber(2); super.setGarage(true); } } |
| Constructing a House Object | ||||
| Calling the new operator creates a new object, first by allocating memory for a new object. In this example when we construct either an object of SmallHouse or LargeHouse we call the new operator along with the class's constructor to initialize the object, but before we call the new operator to construct the object we specify the type of the object (either SmallHouse or LargeHouse) then we specify a variable to reference the object then we use the assignment operator then we would call the new operator followed by the constructor name (always the class name). | ||||
| Type of Object | Variable (Reference to Object) |
Assignment Operator | new Opperator | Class's Constructor |
| LargeHouse | large | = | new | LargeHouse() |
| Going One Step Further | ||||
| To understand how to construct an object, one must be fimiliar with how to create classes or object blueprints. In this example I have implimented inheritance in order to reuse my code. My superclass is House and it is the blueprint for creating both LargeHouse and SmallHouse objects. LargeHouse and SmallHouse are my subclasses and all they consist of is a constructor that calls methods from the super class (House) and specifies explicit paramaters that are needed to set the instance variables in the superclass so that every object that is constructed of that type have the same parameters. And finally you may have noticed that House Id starts at 1000 and increases by one each time a new object is constructed, this is because of a static instance variable in the superclass (House) that keeps track of the number of objects that are created. The static instance variable is specified in the superclass constructor and increases by one everytime a new object is created to show you that the superclass constructor is being invoked even though it isn't specified anywhere when constructing objects of either SmallHouse or LargeHouse. | ||||