Results 1 to 3 of 3

Thread: Extends Issue

  1. #1
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Extends Issue

    I have two classes in which one extends from the other,
    the first one is employee

    Code:
    public class employee {
    
    private String fname;
    private String employeeNum;
    private String hireDate;
    
    public employee(String n, String e, String h) {
      fname=n;
      employeeNum=e;
      hireDate=h;
    }
    
    public static void main(String [] args){
      productionWorker testing = new productionWorker("Joe", "123-Y", "11/15/08", 1, 11.05);
      System.out.print(testing);
    }
    
    
    }
    The next class is production worker.
    Code:
    public class productionWorker extends employee {
    
    private int shift;
    private double payRate;
    
    public productionWorker(String n, String e, String h, int s, double p){
      super(n,e,h);
      shift=s;
      payRate=p;
    }
      
     
     
    }
    I want to be able to output all the information in the new productionWorker, but for some reason when i output it i get this

    productionWorker@27fa03>

    Any insight is greatly appreciated. thanks.

  2. #2
    Join Date
    Oct 2008
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hiya, ive got this working for you but I have a few notes id like to share. According to my course materials a private instance variable is never inherited, so making the var's private is not gonna work proper. Also when you use System.out.print(testing); I think you are just getting the memory location for an object but im not sure tbh.

    To get this to work you need a method to output the info, maybe other ways but it seemed the simplist way. Also your naming of classes and var's seems odd i.e Class(has initial capital) myVar(has capital seperation) ive renamed these for you also. Im new and only just started my java courses so probly better ways to do this but it compiles and works so hey who cares right?

    Here is the code from my 3 .java files hope this helps.

    Main.java
    Code:
    public class Main
    {
       public Main()
       {
       }
       
       public static void main(String[] args)
       {
          ProductionWorker test1 = new ProductionWorker("Joe", "123-Y", "11/15/08", 1, 11.05);
          test1.showProductionWorker();
          ProductionWorker test2 = new ProductionWorker("Sam", "123-Y", "11/15/08", 1, 11.05);
          test2.showProductionWorker();
       }
       
    }
    Employee.java
    Code:
    public class Employee
    {
       
       public String fName;
       public String employeeNum;
       public String hireDate;
      
       public Employee(String n, String e, String h)
       {
          fName = n;
          employeeNum = e;
          hireDate = h;
       }
    }
    ProductionWorker.java
    Code:
    public class ProductionWorker extends Employee {
    
       private int shift;
       private double payRate;
    
       public ProductionWorker(String n, String e, String h, int s, double p)
       {
          super(n,e,h);
          shift = s;
          payRate = p;
       } 
       
       public void showProductionWorker()
       {      
          System.out.println("Emploee Name:" + " " + fName);
          System.out.println("Emploee Shift:" + " " + shift);
          System.out.println("Emploee Number:" + " " + employeeNum);
          System.out.println("Emploee HireDate:" + " " + hireDate);
          System.out.println("Emploee PayRate:" + " " + payRate);
          System.out.println("----------------------------------------");
       }
    }
    Others with more experience maybe able to also provide some thought on my work as im still a newbie.

  3. #3
    Join Date
    Jan 2009
    Location
    Athens, GA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    As Hexbomb indicated, following naming conventions is good practice.
    Generally:
    1. Class names begin with CAPITAL letters. If it is a compound word, Capitalize the first letter of each word.
    2. Non-constant variable and method names begin with lower case letters. If it is a compound word, capitalize the first letter of each additional word.
    3. Constants (indicated by the "final" keyword) are in all CAPITAL letters. IF it is a compound word, separate each word with and underscore (_).

    There are others as well, but I don't want to give you too much at once.

    The reason why this statement
    Code:
    System.out.print(testing);
    output
    Code:
    productionWorker@27fa03>
    is because of how referencing works.
    You'll probably learn about references down the line. It's kind of a hard concept to understand at first.

Bookmarks

Posting Permissions

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