Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By FlipPoker@gmail.com
  • 1 Post By awinston

Thread: Object with different constructors

  1. #1
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Object with different constructors

    I wanna make a grid, which is composed of cells, so I have created a class for object Cell.
    But I want the grid for multiple purposes, so sometimes needs to be a 2D grid and other times a 3D grid, therefore, Cell sometimes needs two field variables: int x & int y; and other times needs three field variables: int x & int y & int z.

    I have tried this:

    Java Code:
    public class Cell {
    
    	int xCoord;
    	int yCoord;
    	int zCoord;
    	
    	public Cell (int x, int y){
    		xCoord = x;
    		yCoord = y;
    	}
    
    	public Cell (int x, int y, int z){
    		xCoord = x;
    		yCoord = y;
    		zCoord = z;
    	}
    }
    The problem with this is that with the first constructor (ex.: new Cell(12,21)) it only initialize two variables, x & y, but a non-initialized variable in java is set to 0 by default, so what I really get is a 3D cell placed on z=0.
    Is there any way in the same object to have ONLY some variables depending on the constructor?

  2. #2
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default Re: Object with different constructors

    In this class, you explicitly declare three data members (xCoord, yCoord and zCoord). You will always have them in any instance of this class. One thing you could do in your first constructor is set zCoord to -1 (or some other invalid number). Then in your grid code, you would check to see if zCoord had an invalid value. That would be your indicator that it is a 2D cell. But this is ugly.

    Another way would be to make Cell your base class and have Cell2D and Cell3D as your derived classes. The base class would have xCoord and yCoord. And only the Cell3D class would have a zCoord.
    awinston likes this.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Object with different constructors

    Quote Originally Posted by FlipPoker@gmail.com View Post

    Another way would be to make Cell your base class and have Cell2D and Cell3D as your derived classes. The base class would have xCoord and yCoord. And only the Cell3D class would have a zCoord.
    I like this idea. A 2D Cell and a 3D Cell are very different and should probably be separate classes.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2011, 08:48 PM
  2. Replies: 1
    Last Post: 05-25-2011, 09:01 AM
  3. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM
  4. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •