1
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

3
  • 3
    Error is right. worker has main, inheritance don't have main. Run worker instead of inheritance. Commented Dec 10, 2015 at 6:46
  • 3
    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. Commented Dec 10, 2015 at 6:46
  • Also the ' symbol at the end may be the problem. Commented Dec 10, 2015 at 6:55

2 Answers 2

0

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; } 
Sign up to request clarification or add additional context in comments.

Comments

-1

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

5 Comments

@stephen can u just tell what is wrong with the answer?The error can happen in all the ways i spaecified
The problem is nothing to do with the public modifier or lack of it. See the first comment for the real explanation. (The OP seems to think that static methods are inherited ....)
I am telling the ways in which the same error can happen.Even with the absence of public modifier it can happen if you are running the same with an IDE,Please have a try and comment.
1) That is an IDE bug then. The Java specs do not requite the class to be public. 2) It is not the OP's problem. Answers should answer the actual question, not provide hints to people who have a >>different<< problem.
Even it is an IDE bug it is the right way to answer as the person who asked the question is a beginner in java and the steps followed for running the program is not mentioned.The same error can caused by different reasons.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.