Results 1 to 11 of 11
  1. #1
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default ArrayList contains two children of a class - how do I set variables of extensions ?

    I create and array list of User objects and fill it with two types of objects created by extending the User object. To try and make this clearer, I have an array of user objects that is filled with basic users (users instantiated using an extension of the user class called basic) and superduper users (users instantiated using an extension of the user class called superduper). In the User class, lets say I have first and last name instance variable. In the basic class, lets say I have first and last name plus one more say age. In the superduper class, lets say first and last name plus address and phone number). As I create objects of both child classes, I create new User instances and put them in a User ArrayList.

    I have gets/setters for first name & last name to change the values in the ArrayList that work fine (they are contained within the User class code). When I go to set child variables in the ArrayList, I get an error.

    I have the getters/setters set up in the basic and superduper classes.

    This is very confussing to me, so if you can ask a question to help me sort this out, that would be good.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    I get an error.
    you need to post the full text of the error message so we can see what the problem is.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    I'm not sure how well we'll be able to help without seeing code and especially without seeing error messages.

  4. #4
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    Below is what the child class looks like, in the parent class, I have getters and setters for all the constructor variables (all defined as protected) other than hairColor and eyeColor.

    In another class, I create new instances of this Basic class with this line:
    userList.add( new Basic(firstName, lastName, userName, userId, passWord, yearsInRole, adminLevel, hairColor, eyeColor ));

    userList is ArrayList of User class.

    at compile time on this line-->>>> userList.get(index).setHairColor( hairColor );

    I get an error message saying cannot find symbol - method setHairColor(java.lang.string)

    I know it has soemthing to do with visibility, but don't know what.


    Java Code:
    public class Basic extends User
    {
        private String hairColor = "";      // variable to represent hair color
        private String eyeColor = "";       // variable to represent eye color
    
        /**
         * Constructor for Basic Users
         */
        public Basic(String firstName, String lastName, String userName, String userId, String passWord, 
                    int yearsInRole, int adminLevel, String hairColor, String eyeColor)
        {
            super (firstName, lastName, userName, userId, passWord, yearsInRole, adminLevel);
            this.hairColor = hairColor;
            this.eyeColor = eyeColor;
        }    
        {
           this.hairColor = hairColor;
           this.eyeColor = eyeColor;
        }
    
        /**
         * Basic setters and getters for the instance variables
         */
        
        public String getHairColor() { return hairColor; }
        public void setHairColor(String hairColor) { this.hairColor = hairColor; }
        
        public String getEyeColor() { return eyeColor; }
        public void setEyeColor(String eyeColor) { this.eyeColor = eyeColor; }    
        
        /**
         * toString 
         * 
         * @return     String result repreesnting hair and eye color 
         */
        public String toString()
        {
           String result = lastName + "\t" + firstName + "\tUser Name is: " + userName + "\tUser Id is: " + userId
                    + "\tAdmin level is: " + adminLevel + "\tYears in role: " + yearsInRole;
           result  = result + "\tEye color:  " + eyeColor + "\t"
                           + "Hair color: " + hairColor;
           return result;        
        }
    }
    Last edited by Jadeclan; 03-05-2012 at 02:35 AM. Reason: adding line numbers

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    userList is ArrayList of User class.
    Is the setHairColor method defined in the User class?

  6. #6
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    No. if I have to do that, what is the purpose of extinding classes?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    Then cast the object to the correct type.

  8. #8
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    Back in a bit, hopefully I can figure out what casting is and where/wehn to use it. You've pointed me to a solution thanks.

  9. #9
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    Think I got it, in two lines, could it be done in one?

    So I changed the offending line from ---->>>> userList.get(index).setHairColor( hairColor );
    to:

    Basic temp = (Basic)userList.get(index);
    temp.setHairColor( hairColor );

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    Why bother?

  11. #11
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList contains two children of a class - how do I set variables of extensions

    Quote Originally Posted by Norm View Post
    Why bother?
    LOL -- Curiosity is what has me programming... and this solution well, is just plain un elelgant :>))

    but IT WORKS!!
    thanks again

Similar Threads

  1. Variables from another class won't assign to main class
    By erthbound0 in forum New To Java
    Replies: 3
    Last Post: 12-15-2011, 10:07 PM
  2. please help with passing variables between class's
    By jasonwucinski in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 01:27 AM
  3. Running extensions other than .class
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 08:57 AM
  4. getting class to recognize variables from another class
    By shadycharacter in forum New To Java
    Replies: 1
    Last Post: 04-26-2010, 10:14 PM
  5. Replies: 1
    Last Post: 01-03-2010, 09:48 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
  •