Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 11th, 2003, 11:59 PM
Registered User
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benni
Default the inherit problem in java code.

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;
}

}







 
Old November 12th, 2003, 12:06 AM
Registered User
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benni
Default

Pls give me some guide,Thank much!

 
Old November 12th, 2003, 07:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old November 12th, 2003, 07:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old November 13th, 2003, 06:11 AM
Registered User
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benni
Default

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.



 
Old November 13th, 2003, 06:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/j...-subclass.html

Cheers

Martyn
 
Old November 13th, 2003, 06:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Binni,

Here two more useful links to help you out.

http://java.sun.com/docs/books/tutor.../packages.html

http://www.artima.com/legacy/answers...essages/8.html

Cheers

Martyn





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with code to "Professional Java Data superea_net Java Databases 0 August 1st, 2006 07:07 AM
Java Code - Wrox - Beginning JAVA - Ivor Horton ponguru Java Databases 3 May 18th, 2006 12:30 PM
Can we Inherit ADODB venki5star Visual Studio 2005 3 March 13th, 2006 10:50 AM
Java code problem Loevet J2EE 5 October 23rd, 2003 08:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.