How can I inherit a class in Bean with package?

S

Sarah

Can anyone please help me. Im having a problem to compile class RedCar.
It gives no error when I remove package.com.MyCar line. Class Car works
fine.
-----------------------------
package com.MyCar;

class Car{
protected int total;

public Car(){ this.total = 10; }
}
}
------------------------------
package com.MyCar;

class RedCar extends Car{
public RedCar(){}

public void getTotal(){ System.out.print("Total car:" + total); }
}
 
T

Thomas Hawtin

Sarah said:
Can anyone please help me. Im having a problem to compile class RedCar.
It gives no error when I remove package.com.MyCar line. Class Car works
fine.

Looks like the source (or sourcepath) is in the wrong place.

The files should be in files com/MyCar/Car.java and com/MyCar/MyCar.java
relative to the sourcepath (which is usually the current directory).

Both files will need to be in the same package as the base class is not
public.

BTW, package names should be all lower case, and preferably a single word.

Tom Hawtin
 
S

Sarah

Usually I put my java files in jdk/bin directory, compile them, then
transfer them to package.

Referring to my problem, is there a way that I can compile those files
from the package?

Btw I have changed the package names.Thanks for the reminder.
 
J

John C. Bollinger

Sarah said:
Usually I put my java files in jdk/bin directory, compile them, then
transfer them to package.

You will eventually find that this approach is too unwieldy to continue
using; this will probably happen sooner rather than later. In the mean
time...
Referring to my problem, is there a way that I can compile those files
from the package?

I'm not sure quite what you mean about compiling files "from the
package." If you want to do the compilation as you describe above then
you can do so by first compiling the Car class, then putting it in an
appropriate (corresponding to the package) directory, then compiling the
RedCar with use of the compiler's classpath option to tell it the base
directory(-ies) in which to look for compiled classes.

If you want to do it the way most of the world does it (directly or
indirectly) then refer to Tom's response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top