• Post Reply Bookmark Topic Watch Topic
  • New Topic

extending a class  RSS feed

 
Elizabeth DeLeon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to java and am having trouble getting the following classes to compile without error.
This first class compiles OK:
public class Book
{
public String title;
public String getTitle()
{
return title;
}
public void setTitle(String newTitle)
{
this.title = newTitle;
}

public Book(String title)
{
this.title = title;
}

}
But this one will not:
public class TechnicalBook extends Book
{
private String skillLevel;
public String getSkillLevel()
{
return this.skillLevel;
}
public void setSkillLevel(String s)
{
this.skillLevel = s;
}
public String toString()
{
return getTitle();
}

public TechnicalBook(String title)
{
super(title);
}
}
I keep getting these two errors:
TechnicalBook.java:3: cannot resolve symbol
symbol : class Book
location : class booklibrary.TechnicalBook
public class TechnicalBook extends Book
TechnicalBook.java:20: cannot resolve symbol
symbol : method getTitle()
location : class booklibrary.TechnicalBook
return getTitle();
Can anybody spot what is wrong?
TIA
[ March 15, 2004: Message edited by: Marilyn de Queiroz ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that your two classes are named Book.java and TechnicalBook.java as separate souce files in the same directory. You have to point your CLASSPATH to that directory so that javac can find the bytecode file Book.class when it is compiling TechnicalBook.java
You can also type: javac -classpath mydirectory TechnicalBook.java
but that's a kluge. you should learn to set your CLASSPATH correctly - you'll need it.
 
Elizabeth DeLeon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sorry I did not specifiy. These are two separate classes in the same directory. Named respectively Book.java and TechnicalBook.java. I have already set up a classpath in my windows enviroment variables. The first class Book.java compiles without error. its only when i try to compile the second that I get the errors.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please display your CLASSPATH and cut and paste onto this page. Also, display the full directory paths to Book.java and TechnicalBook.java and cut and paste onto this page.
Also, please try
javac -classpath c:\yourfulldirectorypath TechnicalBook.java
and give the results.
 
Elizabeth DeLeon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions. Both classes are in c:\jtest2\booklibrary\. I tried:
javac c:\jtest2\booklibrary\TechnicalBook.java
and got the exact same error messages (cannot resolve symbol
symbol : class Book and cannot resolve symbol
symbol : method getTitle()).
The Book.java class compiles OK. Just not TechnicalBook.java. I am using C:\j2sdk1.4.2_01 for my sdk.
I also tried javac -classpath c:\jtest2\booklibrary TechnicalBook.java
and got an error that said "no source files"
Any thoughts? This is very frustrating. I am able to compile all the solitary classes I want but as soon as I try and compile a class that extends another, even when they are in the same folder, I get errors.
TIA
 
Elizabeth DeLeon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I think I got it. Just in case anyone else runs into this. . . you must tell the compiler the two classes are dependent on each other. So this works:
javac booklibrary\Book.java booklibrary\TechnicalBook.java
I had been doing the compilation for each class separately which produced the errors. Thanks for pointing me in the right direction!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you found a workaround, but it shouldn't be necessary to compile your entire class hierarchy with one javac command. This will get pretty tedious as you build larger programs.
Based on what you said, I still think you have an error in your CLASSPATH. You'll have to fix this eventually as many java features depend on it.
If there is no one local to help you, cut and paste your entire CLASSPATH and I'll try to spot the problem. Based on my personal experience, CLASSPATH issues are quite common among java greenhorns.
Good luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!