All of our Java code will be defined in a file ending with the extension .java. This file defines all the classes and interfaces we need. In the previous tutorial, we learned about the definition of a class, in this tutorial, I will only show you how to define multiple classes or interfaces in the same file.
Java allows us to define one or more interfaces or classes in the same file.
We can switch the definition of classes or interfaces in the same file without any problems. In the example above, we can put the Cat class on top as follows:
But it is imperative that you only define one public class or public interface in the same file. In this case, the file name must be the name of the class or public interface.
In the example above, we have defined the Animal interface as a public interface, forcing the file containing our code to be Animal.java. If not, you will be compile error right away.
In case you define multiple classes or interfaces in the same file, the package that contains the classes and interfaces will be the package of all the classes and interfaces that you have defined.
In the above example, the Cat class and Animal interface will be in the same package as com.huongdanjava.