I have just started learning Java and I am learning method overriding. I am getting an error in a program while trying to run it in eclipse. The program is as follows:
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}
The error is:
Error: Main method not found in class Vehicle, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Output should be: Vehicle is running.
I tried to type the statement "public static void main(String[] args)" after "class Vehicle" but getting other errors. I tried the following program:
class Vehicle{
public static void main(String[] args){
void run(){System.out.println("Vehicle is running");}
}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}
Then, I get the following errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token "void", new expected run cannot be resolved to a type System.out cannot be resolved to a type Syntax error, insert "Identifier (" to complete MethodHeaderName Syntax error, insert ")" to complete MethodDeclaration Syntax error, insert ";" to complete MethodDeclaration Syntax error, insert "}" to complete ClassBody This method requires a body instead of a semicolon Syntax error on token "}", delete this token
at Vehicle.main(Vehicle.java:3)
Can anybody please explain which is the right way to insert 'main method' in the above program.
Thx in advance.