1
votes
package myPackage;

public class inheritance {
     int salary = 50000;
}

class worker extends inheritance {
    int bonus = 10000;

    public static void main(String[] args) {
        worker obj1 = new worker();
        System.out.println("employee salary is" + obj1.salary);
        System.out.println("employee bonus is" + obj1.bonus);
    }
}

Hi.. I am new to java. I am trying to write an inheritance program and getting this error.

Error: Main method not found in class myPackage.inheritance, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

2
Error is right. worker has main, inheritance don't have main. Run worker instead of inheritance. - ΔȺȾΔ
TAsk has identified the problem - but please format the code in your post and in future posts; it's really hard to read at the moment. - Jon Skeet
Also the ' symbol at the end may be the problem. - Andrej

2 Answers

0
votes

Try to move the main method inside of inheritance class like this:

public class inheritance {
    int salary = 50000;
    public static void main(String[] args) {
        worker obj1 = new worker();
        System.out.println("employee salary is" + obj1.salary);
        System.out.println("employee bonus is" + obj1.bonus);
    }
}

class worker extends inheritance {
    int bonus = 10000;
}
-1
votes

This error can happen in multiple ways

First need to clarify how you are compiling and running your program

1.Ensure that the java files are placed in the correct package(folder) itself

2.Ensure location of your class file is added to your class path variable

Most probably your problem will be solved by making the inheritance class as public.

package myPackage;
class inheritance {
int salary = 50000;}
public  class worker extends inheritance {
int bonus = 10000;
public static void main(String[] args) {
worker obj1 = new worker();
System.out.println("employee salary is" + obj1.salary);
System.out.println("employee bonus is" + obj1.bonus);
}
}