Results 1 to 5 of 5
  1. #1
    Quintis is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default referencing subobjects with a reference to the base object

    Hello everybody,

    I don't think I've quite understood everything about how to take advantage of the inheritance mechanism.

    My code is quite long and not everything is relevant to the comprehension of the problem, so I will summarize:

    public class Cell{
    .......
    public void update(){
    ........
    }
    }

    public class SideCell extends Cell{
    ......
    public int a;
    public int b;
    .......
    public void update(){
    ........
    a++;
    b++;
    }

    }

    So basically the only difference between Cell and SideCell is the adding of a and b, a and b are not simply incremented each time the update method is
    called, the operations are much more complex but let's assume they are, this is not my main problem.

    I thought that if I were to create a Cell reference, I could reference a SideCell object with this reference as well as a Cell object.
    In my project, I have to set up a 2D array mixing Cells and SideCells.
    But let's assume it is just a 3 cells array, inside my class Main, I have:

    Cell[] cellTable= Cell[3];
    cellTable[0]= new SideCell(...);
    cellTable[1]= new Cell(....);
    cellTable[2]= new SideCell(....);

    But when I want to access the a or b values of my SideCells:

    int var = cellTable[0].a;
    int var = cellTable[2].b

    It tells me the variable cannot be resolved or is not a field.

    So I have to assume that I in referencing SideCells with a Cell reference, all I do is a typecast.

    So my question is this:
    What can I do to contain two kind of objects, one being the descendant of the other in a same array and be able to access the special fields of the subobjects (provided of course that I know where I've placed the sub objects and don't try to access those fields in the base objects)?
    Is this even possible?

    Thank you for helping me!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: referencing subobjects with a reference to the base object

    It's a compiler thing: all that it knows is that you have an array of Cells; it doesn't know that you now and then put a SideCell in that array. It's you who knows that so it's you who has to cast those Cell entries to SideCell entries whereever applicable. You could add methods getA() and getB() to the Cell class that return 0 or -1, and override those methods in the subclass; that way you never have to downcast anything.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Quintis is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: referencing subobjects with a reference to the base object

    Thank you very much for your answer!
    Indeed, I had to cast the array cells to access the variable but it was there:

    int var = ((SideCell)(cellTable[0])).a;

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: referencing subobjects with a reference to the base object

    Quote Originally Posted by Quintis View Post
    Thank you very much for your answer!
    Indeed, I had to cast the array cells to access the variable but it was there:

    int var = ((SideCell)(cellTable[0])).a;
    Yep, that works; to check for mistakes, you can do this:

    Java Code:
    if (cellTable[i] instanceof SideCell)
       var = ((SideCell)(cellTable[i])).a;
    ... but I suggest the second option I mentioned above.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: referencing subobjects with a reference to the base object

    I would question your model if you need access to the inner workings of a Cell subclass.
    Possibly whetever you are using a and b for should be calculated inside SubCell, also inside Cell.
    So, rather than asking for a and b and doing work based on those values, ask Cell to do the work.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Reference object
    By clj89 in forum New To Java
    Replies: 5
    Last Post: 10-23-2011, 12:32 AM
  2. Referencing object from another Thread
    By Floiancu in forum Threads and Synchronization
    Replies: 6
    Last Post: 05-15-2011, 02:20 PM
  3. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 09:39 PM
  4. Replies: 3
    Last Post: 09-29-2010, 10:26 AM
  5. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM

Tags for this Thread

Posting Permissions

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