Java extending class containing main method
I use the following code as part of my assignment
class Base { public static void main(String[] args){ System.out.println("Hello World"); } } public class Factorial extends Base{ }
My task is to run the code and interpret the output The name of the file is factorial java. The code runs without problem, and printing Hello world is amazing to me Before entering the code, I don't think it will compile because the parent class being extended should be in another file, but now I'm not sure Thank soome very much for your clarification
Solution
Java allows you to work in a single Multiple classes are defined in the java file, provided that you can only have one public class at most. If you do so, the name of the public class must be the same as Java file name match In your case, the class declared public is factorial, so your file name must be factorial java.
Inheritance works normally here, and factorial inherits public static void main(), which is why you see your output when executing Java factorial