Learn Java - Part 3

  • Learn About Java Classes :

Ok before we go deep in to class declaration and so topic.. I believe its better to have a proper understanding about the rules associated with declaring classes.

There can only be one public class per source code file.

 Comments can appear at the beginning or end of any line in the source code file.

 If there is a public class in a file, the name of the file must match the same of the public class. For  example : a class declared as public class Dog {} must be in a source code file names Dog.java

  If the class is part of a package, the package statement must be the first line in the source code    file, before ant import statements that may be present.

 If there are import statements, they must go between the package statement(if there is one) and the  class declaration. If there isn't a package statement,then the import statement(s) must be the first line(s)           in the source code file.If there are no package or import statements, the class declaration must be the             first line in the source code file

import and package statements apply to all classes within a source code file.In other words, there's no            way to declare multiple classes in a file and have them in different packages, or use different imports.

 A file can have more than one nonpublic class.

 Files with no public classes can have a name that does not match any of the classes in the file.

Ok now you are equipped for the knowing how to declare a classes in your code. When you want to declare a class i  you code you now use  class key word. as well as you need to add {} after you declaration of the class to indicate the borders of the relevant class. All of the variables , methods (Those will learn later. ) which are relevant to those classes are will be reside in those {} brackets.

Ex :  class dog{} , class man{}


For a declared class you can add access modifiers in front of it. Those are also be the key word of java. Such as public , private and protected. By default any declared class have access modifier default on it. But we don`t indicate that in the code. 
Examples with Access Modifiers : 

public class dog { } ,  private class man { } ,  protected class pen { }

Not only the access modifies you can use Non - Access modifiers as well with a class declaration. Such as strictfp , abstract , final
Examples with Non Access Modifiers :
final class dog {} , abstract class man {}


Now lets move on to class access modifiers..
Class Access.. 

What is mean to access a class?. When we say code from one class (class A) has access to another class (class B) , it means class A can do one of three things.

  1. Create an instance of class B
  2. Extend  class B (that means became a sub class of class B)
  3. Access certain methods and variables with in the class B, depending on the access control of those.
The easiest way to understand about the access is consider the access as visibility. For a example If class A can`t see class B, class A can`t access any of those variables and the methods in class B.

Now Let`s looking to each of the access modifies separately. 

If we are coding a some program with several classes basically we first set up a package. If you use Netbeans for coding that task will be done for you automatically. There can be packages in side packages as well. As well as all of the classes that we create must belong to one of package that you are working on. Each package can have one or more classes. See below figure.


As you can see above there is a package call accounts. (You may not familer with this diagrams. You can learn about UML class diagram representation here) inside that package there are 3 classes called BankAccount  , CheckingAccount and SavingsAccount. As well as in each class represantation you can clearly see variable and methods are separated by a line in that each class representing box. Since those Checking and Savings accounts are the two types of Bank Account we can extend them from its major class BankAccount.  we called it we extend SavingsAccount class from BankAccount class. (we will see how to a class from its super class (parent class) later) One of major advantage of extending parent class is we don`t need to implement same methods and varibals over and over which are common to all of them. In the above diagram you can clearly see that in the BankAccount class we have used a method call withdrawal()
where as which uses same method in the lower level classes. Tough i drew those in this diagram in actual code we don`t want to implement those methods since we do extending the parent class. By extending parent we get all of methods in the parent class by default.

Ok i am pretty sure that now you have basic idea about classes and packages. Ok now lets move to our new session called Access Controls.

There are number of access modifiers to set access level for classes, variables, methods and constructors. There are four major access levels in java. Such as ;

  • Visible to the package only. the default. No modifiers are needed
  • Visible to the class only (private)
  • Visible to the world (public)
  • Visible to the package and all sub classes (protected)
The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether sub classes of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.


The following table shows where the members of the dog class are visible for each of the access modifiers that can be applied to them.










        0 comments :

        Post a Comment