1
votes

Getting runtime time error :- Error: Main method not found in class languagefundament.Vehicle, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Programming done in Java for Abstract class and Abstract method ut while running error comes,

Kindly explain where it has went wrong as i am new to programming and watching lecture on youtube


package languagefundament;

abstract class Vehicle
{
    abstract public int getNoofwheels();
    
}
class Bus extends Vehicle
{
    public int getNoofwheels()
    {
        return 7;
    }
    public static void main(String[] args)
    {
        Bus a = new Bus();
        System.out.println(a.getNoofwheels());
    }
}

2
The main() function always need to be in a public class. - LenglBoy
The main method should be in a public class with the same name as the file. Best to 1) put those classes each in its own file and 2) not try to learn programming through YouTube videos - DontKnowMuchBut Getting Better
Can you help me out with correct code for the same so that i can get it quickly, - Sudhanshu
Also is offline coaching beeter for learning java than youtube videos? - Sudhanshu

2 Answers

-1
votes

this shouldn't be too hard to solve= As you may already know, each program needs some kind of entry point, for java applications this is the so called main method, it is needed to tell Java where to start executing you code.

try to add a file called Main.java with the following code:

public class Main {

    public static void main(String args[]) {
        System.out.println("hello world!");
    }

}

If you are using the commandline you can now do

java Main.java

this should print out "hello world!"

-1
votes

In java, your file should have a "main class"(this is what is throwing your error, not all classes need it), which is declared as "public class main{}"(main will be replaced by your filename, excluding the .java or .txt at the end). Your code doesn't have that main method, which is your error. Java looks into that class to find the "public static void main(String[] args)". You are allowed to have the main method declaration inside another class, but java requires the declaration in the public class(which you don't have here).

Add the following to your code:

package languagefundament;
// assuming your file name is main.txt
public class main{
    public static void main(String[] args){
        Bus a = new Bus();
        System.out.println(a.getNoofwheels());
    }
}

abstract class Vehicle
{
    abstract public int getNoofwheels();
    
}

class Bus extends Vehicle
{
    public int getNoofwheels()
    {
        return 7;
    }
    public static void main(String[] args)
    {
        Bus a = new Bus();
        System.out.println(a.getNoofwheels());
    }
}