Results 1 to 3 of 3
  1. #1
    L0gan is offline Member
    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0

    Default Inheritance super variables help.

    Hey guys, I'm writing a simple program in which I have a super class Person, inherited by the subclasses Customer and Employee (they inherit the variables ID, name and surname).
    Java Code:
    public class Person {
        
        int id;
        String name;
        String surname;
        
        public Person () {
        }
        public Person (int i, String n, String s) {
            id = i;
            name = n;
            surname = s;
        }
    Java Code:
    public class Employee extends Person implements Serializable {
        
        String username;
        String password;
        String date;    
        int hpw;
        int recordSold;
        float hourPay;
        
        public Employee() {
        }
        
        public Employee (String u, String n, String s, String p, int i, int h, String d, int rSold, float hPay) {
            username = u;
            super.name = n;
            super.surname = s;
            password = p;
            super.id = i;
            hpw = h;
            date = d;
            recordSold = rSold;
            hourPay = hPay;
        }
    However the problem is here: when I try to get the variables ID, name and surname through my main class, they fail to return (0,null,null). Why is this? I have get-Methods in my subclasses which should return the super variables, but they are not. Thanks for your time and patience.

    Java Code:
    public String getUser() {
            return username;
        }
        
        public String getName() {
            return super.name;
        }

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Inheritance super variables help.

    You're doing the constructors wrong for one.

    This:

    Java Code:
    public Employee (String u, String n, String s, String p, int i, int h, String d, int rSold, float hPay) {
      super.name = n;
      super.surname = s;
      super.id = i;
      ...
    }
    Must be this:

    Java Code:
    public Employee (String u, String n, String s, String p, int i, int h, String d, int rSold, float hPay) {
      super(i, n, s);
      
      ...
    }
    And you forgot to post the code where you actually construct objects and the code where the values end up being not initialized.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Inheritance super variables help.

    Well, your code worked for me (even though it is not really designed correctly). In addition
    to what Gimbal2 said you need to let the super class provide its own methods for accessing
    its own instance fields. That way, you don't need to override them (or even write your own)
    since they do what they are supposed to do. And remember that instance fields are
    different than methods. You cannot override them. So if you have an accessible subclass field
    called name and an accessible super class field called name, the subclass field will hide the super class
    field. But they are still distinct fields.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Inheritance and instance variables.
    By garyiskidding in forum New To Java
    Replies: 5
    Last Post: 05-21-2013, 02:02 PM
  2. Replies: 2
    Last Post: 06-03-2011, 10:15 PM
  3. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 02:21 AM
  4. Replies: 3
    Last Post: 03-14-2009, 08:22 AM
  5. Inheritance and (super)
    By pheonix in forum New To Java
    Replies: 12
    Last Post: 10-20-2008, 04:00 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
  •