Subject: the inherit problem in java code.
Posted By: benni Post Date: 11/11/2003 10:59:13 PM
i write a small java program about class Inherit but it compiler have error.
i have two question: if i can not put class and its subclass in one file?
if i can not call the object that define in superclass?

Error message:

F:\javastudy>javac TestInherit.java                         TestInherit.java:8: cannot access Employee
file Employee.class not found                                   Employee p2=new Manager();                         
    
 .\Mypackage\Employee.java:56: cannot resolve symbol   
      symbol  : variable birthday                                 location: class Mypackage.Manager                               System.out.println("it is Manager class"+birthday.year);                                             ^              2 errors                                                    



*TestInherit.java
import Mypackage.MyDate;
import Mypackage.Employee;
public class TestInherit
{
public static void main(String[] args)
{
Employee p1=new Employee();
Employee p2=new Manager();
p1.getDetails();
p2.getDetails();
}
}
*Employee.java
package Mypackage;
public class Employee {
private String name;
private double salary;
private MyDate birthDate;

public Employee(String n,MyDate DoB) {
name=n;
birthDate=DoB;
}

public Employee(String n,double m) {
name=n;
salary=m;
}

public Employee(String n) {
this(n,null);
}


public Employee() {
name="goodman";
salary=2000000.00;
MyDate birthday=new MyDate();
}

public String getDetails() {
System.out.println("it is Employee class");
return "name: "+name+"\nsalary "+salary;
}
}

class Manager extends Employee {
private String department;

public Manager(String name,double salary,String dept) {
super(name,salary);
department=dept;
}

public Manager(String n,String dept) {
super(n,null);
department=dept;
}

public Manager(String dept) {
department=dept;
}

public Manager() {
super();
department="aa";
}
public String getDetails() {
System.out.println("it is Manager class "); /* +birthday.year);*/
return super.getDetails() +"\ndept "+department;
}
}
*MyDate.java
package Mypackage;
public class MyDate{
private int day=1;
private int month=2;
private int year=2000;

public MyDate(){
this.day=day;
this.month=month;
this.year=year;
}

public MyDate(int x,int y,int z){
day=x;
month=y;
year=z;
}
public MyDate(MyDate date){
this.day=date.day;
this.month=date.month;
this.year=date.year;
}

public void setDay(int newday){
day=newday;
}
public void setMonth(int newMonth){
month=newMonth;
}
public void setYear(int newYear){
year=newYear;
}
public MyDate addDays(int days){
MyDate new_date=new MyDate(this);
new_date.day=new_date.day+days;
return new_date;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}

}







Reply By: benni Reply Date: 11/11/2003 11:06:40 PM
Pls give me some guide,Thank much!

Reply By: Martyn Reply Date: 11/12/2003 6:47:45 AM
The problem you are getting is due to your package statements. Assuming your code exits in javastudy then your code should compile if you do the following, which will place your class files into the mypackage directory in javastudy.

c:\javastudy> javac -d c:\javastudy *.java

c:\javastudy> java mypackage.TestInherit

By the way; it is common practice to use all lower case for package names.

I hope this helps

Martyn
Reply By: Martyn Reply Date: 11/12/2003 6:54:51 AM
Sorry, I have just read my reply and I might have confused you. There is actually nothing wrong with your package statements (apart from the capitalisation), but the fact that you have included them was causing you a problem, as the compiler was looking for the classes in mypackage, whereas you were compiling into javastudy.

I hope this clears things up and that I haven't added to your confusion.

Regards

Martyn
Reply By: benni Reply Date: 11/13/2003 5:11:50 AM
Thank Martyn. I find problem:
Because i define the superclass and its subclass in one file, and i invoke it out of package, it can not be access from outside package.
now how can i solve this problem? if i must write subclass in another file?


Pls give me guide, Thank much.



Reply By: Martyn Reply Date: 11/13/2003 5:42:56 AM
Benni,

You need to make the Manager class protected, currently you are not specifying an access modifier,therefore it will default to package. See the following url for a more detailed explanation:

http://www.javaworld.com/javaworld/javaqa/2001-04/04-qa-0427-subclass.html

Cheers

Martyn
Reply By: Martyn Reply Date: 11/13/2003 5:49:07 AM
Binni,

Here two more useful links to help you out.

http://java.sun.com/docs/books/tutorial/java/interpack/packages.html

http://www.artima.com/legacy/answers/Jul2000/messages/8.html

Cheers

Martyn

Go to topic 6390

Return to index page 1006
Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997