Results 1 to 16 of 16
  1. #1
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default [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):

    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; }
    
    	}
    
    }
    This class compiles correctly.

    The second class is the first subclass of Vehicles, "Cars":

    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;
    	}
    
    }
    If I try to compile this second class, I get the following error:

    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 errors
    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?
    Last edited by jDennis79; 08-02-2010 at 09:31 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    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.

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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 08:57 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    @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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    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 09:10 PM.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    C:\whatever>javac -cp classpath Cars.java

    classpath is the path to the folder containing the vehicle folder
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    This doesn't work. The result is the same error message as above. :-(

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    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.

  9. #9
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    Instead of the full absolute path for the classpath, try using a relative path that goes up one level: ..\. See bottom of post#5

  11. #11
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Works. Thank you very much! Why doesn't the absolute path work?

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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

  13. #13
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    But shouldn't it work with

    Java Code:
    javac -classpath "Users/dennis/Entwicklung/Java/Einteigerseminar/Kap_05/" Auto.java
    then? Because it doesn't.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    Sorry, I don't know linux.

    Have you tried it without the "s

  15. #15
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    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!

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    Classpath is a mystery for many for a long time.
    When using packages you have to understand it.

Similar Threads

  1. Subclass help
    By amystauff in forum New To Java
    Replies: 1
    Last Post: 05-30-2010, 05:36 AM
  2. Object is a Subclass of
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:42 AM
  3. Subclass name to be a parameter?
    By Peetahzee in forum New To Java
    Replies: 6
    Last Post: 12-12-2009, 04:51 PM
  4. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-21-2009, 12:46 AM
  5. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •