Friday, 4 January 2013

JAVA CLASSES - FUNDAMENTAL OF JAVA

CLASS
Classes is one of that logical construct upon which java language depend. Every thing is done by classes in JAVA.
                        Class act as a template for defining the shape and behaviour of object.  Any concept must be encapsulated in classes in JAVA language. Logically class defines a new data type.  After defining this new type can be used by creating object,s of that class.

GENERAL SYNTAX/FORM OF CLASS
Generally class contain data known as "member data" and code which operate on that data known as "member method".
                     A class is defined by  using a "class " keyword. Its general syntax is :-
                                         class classname
                                         {
                                          (data)type instance -var 1;
                                          type instance - var 2;
                                           ------------------------;
                                          type instance -var N;
                                         
                                          type methodname(Parameter list)
                                          {
                                            //body of method;
                                           }
                                         -------- type method-N()
                                          {  // body  }
                        
                                           }           (here class end)         

  • The data or variable defined inside class known as instance variables.
  • It is the methods that determine how the class,s data can be used and act as a interface.
  • They are known as instance variable because each instace of the class contain its own copy of variables.
  • Consider an example of making class "rectangle" :-
                                       class rectangle
                                         {
                                            int length;
                                            int breadth;
                                          }
  1. This class contain no method. It includes two instance variables namely length and breadth of type "int" an integer.
  2. It is necessary to know this definition or code not create any object. It just define the template of objects which have length or breadth.
  3. To create object we use "new " keyword as :-
                                 rectangle r1= new rectangle();
 This will physically create "r1" object of class rectangle.
                  Now r1 has their own copy of instance variables and if we define r2 they have their own.

  • To access instance variables or methods we use dot(.) operator.  for ex :-
                                 r1.length=100;
would assign 100 to the length of rectangle r1.   
JUST AS SHOWN ABOVE WE CAN MAKE CLASS OF RECTANGLE OR ANY THING INSTEAD OF TREE AND CREATES DIFFERENT INSTANCE OF HAVING DIFFERENT DIMENSIONS.
                                                                             

No comments:

Post a Comment