PDA

View Full Version : Extends Issue



SChaput
11-20-2008, 07:05 PM
I have two classes in which one extends from the other,
the first one is employee



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.



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.

Hexbomb
11-25-2008, 08:29 AM
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


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


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


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.

imaginationac
01-28-2009, 08:03 PM
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
System.out.print(testing);
output
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.