//Vehicle.java
import java.util.Random;
class VehicleConstructor{
private int speed;
private int timeDriven;
private int run;
private int startSpeed; //Pradinis greitis
public VehicleConstructor() {
setSpeed();
System.out.println("Vehicle");
}
private void setSpeed(){
int minSpeed = 1;
int maxSpeed = 40;
Random random = new Random();
this.startSpeed = random.nextInt(maxSpeed - minSpeed + 1) + minSpeed;
}
protected int getSpeed(){
return startSpeed;
}
}
class Bicycle extends VehicleConstructor{
public void Bicycle(){
System.out.println(getSpeed());
}
}
public class Vehicle{
public static void main(String[] args){
Bicycle bicycle = new Bicycle();
}
}
I want to create object Bicycle
which extends VehicleConstructor
and call method getSpeed()
from parent. The problem is that i get an error:
Error: Main method not found in class Bicycle, please define the main method as: public static void main(String[] args)
As far as i know in Java constructors are with the same name as the class, so i have
public void Bicycle(){
System.out.println(getSpeed());
}
But error says that i need public static void main
, and so the other classes also need to be with static
, but i don't want this. Is it possible somehow create object without need of static method in it?