Now when i run the code it gives me an error that the class ProtTest should be declared as a seperate file known as ProtTest.java . I wanna know that why is it so i mean why is this rule applied??
You can but you need to save your file as ProtTest.java.
So in this case, RunProt should be public class, and ProTest non-public one; file should be named 'RunProt.java'.
However, a .java source file can contain a maximum of one top-level class definition that is public. (It is not required to contain any.) If there is a public top-level class, then the source file must share the name of that class.
In your example, ProtTest and RunProt are both defined as top-level classes in the same source file. That's fine.
ProtTest is public, and RunProt has default access. That's fine too.
However, because ProtTest is public, the name of the source file needs to be ProtTest.
Arek Sokolowski wrote:And one more thing - main() method have to be located in public class. Otherwise it will not be launched...
The main method needs to be public, but it can be in a class that has default access.
budsy remo wrote:@ Mark Weber So can i say that in a way by providing just one public class , the concept of encapsulation has been applied ?
Encapsulation would require access restrictions to the members of the class. Providing a non-public class means you are restricting access to that class. A bit like encapsulating that class inside a package, but I don't think we call that encapsulation. At least I think not . . . Have a look in Wikipedai for Encapsulation_(computer_science) and see how it is defined there.
Campbell Ritchie wrote:...I don't think we call that encapsulation...
I agree that reducing the number of public classes is not exactly encapsulation, although I can see how it might look like encapsulation.
As Campbell said, I think encapsulation is more about restricting access to class members to protect against direct manipulation of data. It's more about "internalizing" the inner workings of a class, while providing public methods for users of that class.
I wasn't certain.
I earlier also wrote: . . . At least I think not . . .