• Register
0 votes
44 views

Problem:

Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice:

class Institutions {
    public void print_institutions()
    {
        System.out.println("This is an institution");
    }
} 
class School extends Institutions{
    public void print_school()
    {
        System.out.println("This is a school");
    }
}
class Academy extends School {
    public void print_academy()
    {
        System.out.println("This is an academy");
    }
} 
class Call{
     public static void main(String[] args) {
          Institutions institute = new Institutions(); 
          School school = new School();
          Academy academy = new Academy();
          
          institute.print_institutions();
          school.print_school();
          academy.print_academy();
     }
}

When I run this program, the compiler throws the following error:

Error - At least one public class is required in main file

I have never seen this type of error before, therefore, I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!

10 6 3
6,230 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

Your program does not have any issues. If you try running it on an online compiler, it does not generate any error. But, the solution to this error is quite simple, as the error itself says that there must be a public class in the main file, then you must set the access modifier of your "Call" class to the public. Once your "Call" class is public, this error will not be thrown by the compiler. 

The other solution to this error, in general, is to set the name of your file same as the public class of your program. For instance, if you have a file named "Test.java" and you have a public class named "Hello", this error will generate in this scenario too. So, try to save your file with the public class’s name.

12 5 4
19,060 points

Related questions

0 votes
1 answer 29 views
29 views
Hi, when i try to execute this code i always get the error message from above, and i really want to know why, already tried to put a “public class XXXX” before the code, but not work anyway. public static String invertirPalabras(String cadena) { cadena += " "; String ... + palabra; if (cadena.charAt(i) == ' ') { nuevaCadena += palabra; palabra = ""; } } return nuevaCadena.trim(); }
asked Aug 2, 2020 Daniel Aguilar
3 votes
1 answer 1.6K views
1.6K views
Problem: Hello Kodlogs, I am a novice Java programmer. I just wrote my very first program a few minutes earlier. Now I want to write a program that can add two different integer numbers for me. I guess I done my coding well but it says something wrong with my writing and throws back an error: &ldquo ... []args){ int x = 10; int y = 15; int sum = x+y; System.out.println("Sum is: " + sum); } }
asked Mar 23, 2020 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
Problem: I am trying to write a program, but I'm getting this compiler error: Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java public class WeatherArray { ^ 1 error I have checked my file names, and my public class is the ... How can I fix this? Here is my code: public class WeatherArray { public static void main(String[] args) { // ... } }
asked Feb 15 Ali 5.4k points
0 votes
1 answer 5 views
5 views
Problem: The code is public class Multiply { public static Double multiply(Double a, Double b) { return a * b } } I cannot solve the above code. I tried a few things, like public class Multiply { public double multiply(double a, double b) { return a * b;} }
asked Feb 22 Ali 5.4k points
0 votes
1 answer 57 views
57 views
Problem : I have following two classes: JDialog and JFrame (see signature below). I want them to have few extra methods, but I also want them both to extend my code, without rewriting the code Is it possible to do that? java.awt.Window java.awt.Dialog javax.swing. ... the identical way. So I will not want to write the code twice. I want only one class to utilize the code of the other class.
asked Dec 7, 2019 alecxe 7.5k points
0 votes
1 answer 11 views
11 views
java file with a default class access specifier get compiled?
asked Jan 24 TeamScript 13.5k points
1 vote
1 answer 20 views
20 views
Problem: I have a very basic question being a novice. It might be silly to you but I really want to know the fact. Let’s say you have a java program. Can you have two classes in one java file? What is the reason behind it? Thanks for your clearance.
asked Mar 27, 2020 Gavin 15.3k points
0 votes
1 answer 60 views
60 views
Problem : I am facing following Error. Status 1 error, param missing, error because at least one required parameter is missing. Can anybody tell me the correct status code to give when the parameter is missing from the request? Is 400 the correct one? Or is it 404? [GET /search?q=ok] => 200 OK [GET /search] => 400 Bad Request? or 404 Not Found? Or 422 Unprocessable Entity? Others?
asked Dec 2, 2019 alecxe 7.5k points
0 votes
1 answer 12 views
12 views
Problem: This is more a FYI: When initiating a new Project and implementing your Readme step by step I got this issue: /MainActivity.java:34: error: onNewIntent(Intent) in MainActivity cannot override onNewIntent(Intent) in ReactActivity protected void onNewIntent(Intent intent){ ^ attempting to assign weaker access privileges; was public 1 error :app:compileDebugJavaWithJavac FAILED
asked Feb 15 Ali 5.4k points
0 votes
1 answer 25 views
25 views
Problem: Hi there! I recently attempted an exam of Programming in Java. There was a statement that was to be marked as true or false and I had to justify my answer. The statement was: A protected member of a class may be directly accessed by ... concepts in programming and I am not sure whether I understand inheritance properly. Can anybody here help me clear my concepts more accurately? Thanks
asked Dec 27, 2020 Code Learner 6.2k points