Results 1 to 16 of 16
Thread: subclass won't compile - why?
- 08-02-2010, 07:31 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
[SOLVED] subclass won't compile - why?
Hi everybody,
I am trying to learn Java with a German book from 2010. In the chapter I am currently working on, the author introduces the creation and inheritance of classes and subclasses.
He gives a (very simple) example of a management software for car rental. So far, he created two classes. The first class is meant as an abstract class for vehicles in general. The code (I translated variable names to English):
This class compiles correctly.Java Code:package vehicle; abstract class Vehicles { protected String name; protected short speed; protected boolean rented; abstract boolean newEntry(); abstract boolean showEntry(); public boolean rentCar() { if (rented) { return false; } else { rented = true; return true; } } }
The second class is the first subclass of Vehicles, "Cars":
If I try to compile this second class, I get the following error:Java Code:package vehicle; import java.util.Scanner; public class Cars extends Vehicles { public short numberOfSeats; public boolean newEntry() { Scanner entry = new Scanner(System.in); try { System.out.print("Description: "); name = entry.nextLine(); System.out.print("Number of seats: "); numberOfSeats = entry.nextShort(); System.out.print("Max. speed: "); speed = entry.nextShort(); return true; } catch (Exception e) { System.out.println(e); return false; } } public boolean showEntry() { System.out.println("The car is a " + name); System.out.println("It has " + numberOfSeats + " seats."); System.out.println("Max. speed (mph): " + speed); if (rented) { System.out.println("It is not available."); } else { System.out.println("It is available."); } return true; } }
My guess was that there is a problem with the class and variable specifiers, but the code listings match with the author's explanations in the text, so I don't see the problem here. Is there any mistake?Java Code:Cars.java:5: cannot find symbol symbol: class Vehicles public class Cars extends Vehicles { ^ Cars.java:14: cannot find symbol symbol : variable name location: class vehicle.Cars name = entry.nextLine(); ^ Cars.java:18: cannot find symbol symbol : variable speed location: class vehicle.Cars speed = entry.nextShort(); ^ Cars.java:27: cannot find symbol symbol : variable name location: class vehicle.Cars System.out.println("The car is a " + name); ^ Cars.java:29: cannot find symbol symbol : variable speed location: class vehicle.Cars System.out.println("Max. speed (mph): " + speed); ^ Cars.java:31: cannot find symbol symbol : variable rented location: class vehicle.Cars if (rented) { ^ 6 errorsLast edited by jDennis79; 08-02-2010 at 08:31 PM.
- 08-02-2010, 07:53 PM #2
The problem is with packages.
The compiler needs the classpath set to the folder container the folder at the beginning of the package path.
For a quick test, comment out all the package statements and try compiling the code.
- 08-02-2010, 07:55 PM #3
Are you sure Norm? He should get the same if Vehicles isn't compiled, no? Both classes are in the same package.
Last edited by PhHein; 08-02-2010 at 07:57 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-02-2010, 07:59 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
@PhHein - yes, I did.
@Norm - you're right, after commenting out the package statements, both classes compile correctly. That raises two new questions: 1) Why did Vehicles.java compile nevertheless? Shouldn't there be an error too? 2) How do I set the classpath?
- 08-02-2010, 08:01 PM #5
I'll try it.
For the compiler to find vehicle.Vehicles the classpath must point to where the vehicles folder is, not inside it where the . (current directory)
Here are two compile results screens. First with classpath only in the current folder:
Running: D:\Java\jdk1.6.0_02\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;. Cars.java
Cars.java:5: cannot find symbol
symbol: class Vehicles
public class Cars extends Vehicles {
^
Cars.java:14: cannot find symbol
symbol : variable name
location: class vehicle.Cars
name = entry.nextLine();
^
Cars.java:18: cannot find symbol
symbol : variable speed
location: class vehicle.Cars
speed = entry.nextShort();
^
Cars.java:27: cannot find symbol
symbol : variable name
location: class vehicle.Cars
System.out.println("The car is a " + name);
^
Cars.java:29: cannot find symbol
symbol : variable speed
location: class vehicle.Cars
System.out.println("Max. speed (mph): " + speed);
^
Cars.java:31: cannot find symbol
symbol : variable rented
location: class vehicle.Cars
if (rented) {
^
6 errors
1 error(s)
************************************ HERE I changed the classpath to go up (..\.) one level: <<<<<<<<<
Running: D:\Java\jdk1.6.0_02\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\. Cars.java
0 error(s)Last edited by Norm; 08-02-2010 at 08:10 PM.
- 08-02-2010, 08:04 PM #6
C:\whatever>javac -cp classpath Cars.java
classpath is the path to the folder containing the vehicle folderMath problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-02-2010, 08:10 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
This doesn't work. The result is the same error message as above. :-(
- 08-02-2010, 08:12 PM #8
Can you run the commands in a command prompt?
Copy and paste the screen here.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
- 08-02-2010, 08:17 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
Java Code:javac -classpath "Users/dennis/Entwicklung/Java/Einteigerseminar/Kap_05/fahrzeug/" Auto.java Auto.java:5: cannot find symbol symbol: class StrassenFzg public class Auto extends StrassenFzg { ^ Auto.java:14: cannot find symbol symbol : variable bez location: class fahrzeug.Auto bez = eingabe.nextLine(); ^ Auto.java:18: cannot find symbol symbol : variable geschw location: class fahrzeug.Auto geschw = eingabe.nextShort(); ^ Auto.java:27: cannot find symbol symbol : variable bez location: class fahrzeug.Auto System.out.println("Das Auto ist ein " + bez); ^ Auto.java:29: cannot find symbol symbol : variable geschw location: class fahrzeug.Auto System.out.println("Höchstgeschwindigkeit (km/h): " + geschw); ^ Auto.java:31: cannot find symbol symbol : variable verliehen location: class fahrzeug.Auto if (verliehen) { ^ 6 errors
- 08-02-2010, 08:24 PM #10
Instead of the full absolute path for the classpath, try using a relative path that goes up one level: ..\. See bottom of post#5
- 08-02-2010, 08:30 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
Works. Thank you very much! Why doesn't the absolute path work?
- 08-02-2010, 08:33 PM #12
Because you have defined the package fahrzeug. So the compiler is looking for Users/dennis/Entwicklung/Java/Einteigerseminar/Kap_05/fahrzeug/fahrzeug/ which doesn't exist.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-02-2010, 08:37 PM #13
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
But shouldn't it work with
then? Because it doesn't.Java Code:javac -classpath "Users/dennis/Entwicklung/Java/Einteigerseminar/Kap_05/" Auto.java
- 08-02-2010, 08:46 PM #14
Sorry, I don't know linux.
Have you tried it without the "s
- 08-02-2010, 09:02 PM #15
Member
- Join Date
- Aug 2010
- Posts
- 23
- Rep Power
- 0
OS X. :-) Okay, still Unix. - Yes, I did. Whatever. It works with relative path for now. I will try and find some internet sources about the whole classpath matter. I only wonder why the author doesn't explain it in his book - since it's already the 6th edition. :-)
Thanks guys!
- 08-02-2010, 09:05 PM #16
Similar Threads
-
Subclass help
By amystauff in forum New To JavaReplies: 1Last Post: 05-30-2010, 04:36 AM -
Object is a Subclass of
By AndrewM16921 in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:42 AM -
Subclass name to be a parameter?
By Peetahzee in forum New To JavaReplies: 6Last Post: 12-12-2009, 03:51 PM -
subclass troubles
By xf021209 in forum New To JavaReplies: 12Last Post: 04-20-2009, 11:46 PM -
Subclass definition
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks