Reputation: 121
I have the following code, that works:
public class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue){
cadence = newValue;
}
void changeGear(int newValue){
gear = newValue;
}
void speedUp(int increment){
speed = speed + increment;
}
void applyBrakes(int decrement){
speed = speed - decrement;
}
void printStates(){
System.out.println("cadence: " + cadence + "; speed: " + speed + "; gear: " + gear);
}
public static void main(String[] args) {
// Create two different 'Bicycle' objects:
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on the two new objects:
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(45);
bike2.speedUp(15);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
Then I want a new class called 'MountainBike' to inherit from the class 'Bicycle', and I wrote the following new code:
package mountainbike;
public class MountainBike extends Bicycle {
public static void main(String[] args) {
// TODO code application logic here
}
}
But here I get an error indication on the line of public class MountainBike extends Bicycle {
saying cannot find symbol class Bicycle
.
I suppose I have to put an "import" somewhere, but what should I import exactly?
Upvotes: 3
Views: 187
Reputation: 2123
Put Bicycle class and MountainBike class in the same package, mountainbike package maybe. Or import the package of Bicycle class on the file that defines the MountainBike class.
package mountainbike;
import bicycle.*; //or import bicycle.Bicycle;
public class MountainBike extends Bicycle {
public static void main(String[] args) {
// TODO code application logic here
}
}
Check also the folder structure. Bicycle class should be in 'bicycle' folder, that corresponds to the bicycle package and MountainBike class should be in 'mountainbike' folder, that corresponds to the 'mountainbike' package. Your project structure should be the similar.
Upvotes: 0
Reputation: 109613
Move Bicycle to some package aka subdirectory. The default package (leaving out package ... and using the top directory) is not intended for such usage (yeah).
Especially one cannot do:
import Bicycle; // NO CAN DO - Default package import.
Being able to use a fully qualified name mountainbike.MountainBike
but not being able to use Bicycle
was a measure to ensure that packages were used to structure classes.
Upvotes: 0
Reputation: 4360
Import the package containing Bicycle class, add this to your MauntainBike code:-
package mountainbike;
import packageName.Bicycle;
public class MountainBike extends Bicycle {
public static void main(String[] args) {
// TODO code application logic here
}
}
Or If there is no package for the Bicycle class (default package),add Bicycle to the same package as that of MountainBike
package mountainbike;
public class Bicycle {
.
.
.
}
Now Bicycle is in the same package and hence visible to the MountainBike class.
Upvotes: 2
Reputation: 1287
If you have your MountainBike
class in package mountainbike
, and your Bicycle
class is not in this mountainbike
package, you can get this error. One solution would be to import the package in which Bicycle
is located. You can either type the import like
import com.example.bicycle.Bicycle;
But usually packages are used to separate unrelated classes and keep related classes together. I feel that the classes you are dealing with are related enough to be in the same package. If both are in the same package your code will work without any extra import.
Upvotes: 0
Reputation: 2209
Is your package for MountainBike
and Bicycle
same ?
If not import the Bicycle
in your MountainBike
class.
import <bicycle-package>.Bicycle;
Upvotes: 0