-1

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.

Rahul
  • 9
  • 4

2 Answers2

1

this is wrong:

class Vehicle {
    public static void main(String[] args){
 void run(){System.out.println("Vehicle is running");}
    }
}

you cant just nest a method definition inside another method like that...

you can do something like:

public class Test {

    public static void main(String args[]) {
        Bike obj = new Bike();
        obj.run();
    }

}

class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Bike extends Vehicle {

}

and be sure the class you are running to start the application is the test class (the one with the static void main method)

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I tried the above program, but it still shows the same error. It also shows that public type Test must be defined in its own file. If I remove public also, it still shows the same error. 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" – Rahul Jun 27 '17 at 07:23
0

You were correct the first time:

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();  
    }
}  

This does run I have just tested it, this was my output:

Vehicle is running
Process finished with exit code 0

May I suggest that you try doing a "clean & build" before running.

MartinByers
  • 1,240
  • 1
  • 9
  • 15