Logo

dev-resources.site

for different kinds of informations.

Java Error - at least one public class is required in main file

Published at
3/16/2022
Categories
java
class
inheritance
public
Author
coderlegi0n
Categories
4 categories in total
java
open
class
open
inheritance
open
public
open
Author
11 person written this
coderlegi0n
open
Java Error - at least one public class is required in main file

πŸŽ‰ Before you dive into this article...

πŸš€ Check out our vibrant new community at CoderLegion.com!

πŸ’‘ Share your knowledge, connect with like-minded developers, and grow together.

πŸ‘‰ Click here to join now!

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();
 }
Enter fullscreen mode Exit fullscreen mode

}
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!

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 the 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.

I hope this will help you
Thanks

inheritance Article's
30 articles in total
Favicon
Code Smell 286 - Overlapping Methods
Favicon
Understanding Traits in PHP and How They Differ from Inheritance
Favicon
Understanding Classes and Inheritance in JavaScript
Favicon
Mastering Generalization in OOP: Techniques and Examples
Favicon
Upcasting β€” Using a Superclass Reference for a Subclass Object
Favicon
Mapping inheritance hierarchies with MapStruct
Favicon
Python Inheritance Explained: Types, Examples, and Best Practices
Favicon
Mastering TypeScript: Understanding the Power of extends
Favicon
PHP: Herança vs. Composição
Favicon
Method Resolution Order in Python 3
Favicon
Why we don't use RemoteWebDriver driver = new ChromeDriver()
Favicon
Understanding JavaScript Inheritance: A Deep Dive into Prototypal and Constructor Patterns
Favicon
Understanding Inheritance and Polymorphism: Simplified OOP Concepts
Favicon
JS Inheritance - Part 2: Factory Functions vs. Classes
Favicon
Inheritance with access-specifier in cpp
Favicon
Inheritance in Dart
Favicon
RoR - extend, < (inheritance), include - know the difference
Favicon
Inheritance vs composition: a fight against Egyptian gods
Favicon
Object Oriented Concept - Inheritance
Favicon
Exploring Component Inheritance in Aventus
Favicon
Python Inheritance
Favicon
Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example
Favicon
PostgreSQL - Partitioning & Inheritance
Favicon
Prototype and Prototypical Inheritance
Favicon
Ruby - Inheritance
Favicon
MultiLevel Inheritance in Java
Favicon
Overloading vs Overriding in Typescript
Favicon
Java Error - at least one public class is required in main file
Favicon
Polymorphism in Java.
Favicon
Method Overriding in Java.

Featured ones: