Saturday, April 3, 2010

Element Access :: Java

  1. public class can not be declared in any java file. File name should match with public class name. So, in case you are planning to write multiple class in one file (which is not a good practice.) then in the java file only 1 public class could exists.
  2. Can a class have "private access" ?
    Only nested class can have private/protected access. OUTER class can have only public/default access. Private/protected access is basically meant for the member elements.
  3. An interface can have only public/default access.
  4. Changing access of any element from HIGHER to LOWER will result into compilation error.
  5. If interface is public, then all of its member variable/methods will have same access. And access of member elements can not be changed also.
  6. Variables defined in interface are implicitly static and final.
  7. Methods that implement interfaces must be declared as public. (Fun part is if you forget to do that JAVAC is there to remind you.)

Order of different access is as follows:

PUBLIC --> Protected --> Default --> Private

No comments:

Post a Comment