24 September 2012


Tutorial #7

   Hello friends in this tutorial we will mainly see how Classes & objects are being created in  
   java & ways of creating Method inside the class.

   Class & Objects
   In common language Class can be considered as user defined data-type of which Objects 
   are variable.
            It can also be considered as the black-box that contains both data & method & we just 
   need to invoke it with the help of variable without knowing how internally method really does 
   the work, we are simply interested in its output for a particular sets of inputs.

            Syntax:
            Class classname {extends superclass}
            {
                        Data item declaration;
                        Method declaration;
            }

   Note:- C++ programmer please kept in mind that there is no semicolon after closing brace 
   as it would have been in C++ class declaration.

**         Field (or data) declaration
            Data is declared inside the class in the following way-
                        class Rectangle
                        {
                                    int length;
                                    int  value;
                        }

            Methods declaration
            The syntax of declaring method inside the class is shown below-
                        class
                        {
                                    Type methodname (parameter- list)
                                    {
                                                Method body;
                                    }
                        }

   Note-   1. If your method is returning nothing then simply put “void” before method-name in               place of type in the above syntax;

              2. The parameter list used in the method header should always be declared           
              independently separated by commas.
                            E.g=> void getData(int x,w)                //invalid

              3.Instance variables and methods in the classes are accessible by all the method in the
              class but method cannot access variables declared in other class with local scope.

**         Creating object
            The syntax of creating an instance of the class (or object is given below)-
                        ClassName   ObjectName;                            //declare the object;
                        ObjectName =new ClassName();                  //instantiating the object

                                    E.g=> house room;
                                                room= new house();

**         Constructors
             Constructor are the functions whose name is same as the name of the class & mainly
             used to initialize the data contained in the class . They also do not specify any return
             type, not even void. This is because they return the instance of the class itself.
                        E.g=> class house
                                    {
                                                int length;
                                                house (int x)                //constructor method
                                                {
                                                            length =x;
                                                }
                                    }  

     Constructor are mainly of two types 

;-





1.                  Default constructorthis type constructor initializes all the objects of a given lass to same
            value.
                It is also of two types

1.1       System defined-  this type of constructor is being class if the user defined default
   or   parameterized constructor is being called. 

1.2      User defined- this type of constructor initializes all the obect defined for a given
         class with the same value defined by the used once
.
2.               Parametrerized constructor- This constructor initializes all the objects with different values
          as specified while declaring that object.

            E.g=>  Example of constructors:-

            Class peri {
                        int length, breadth;

            //default constructor   
            peri()
            {
                        length =0;
                        breadth=0;
            }          

            //parametrized constructor
            peri(int x , int y)
            {
                        length=x;
                        breadth=y;
            }

            Public static void main (String args[ ])
            {
                        peri a1= new peri();
                        peri a2 = new peri (5,10)
                        System.out.println(“the default value of length=”+a1.length);
                        System.out.println(“the parametrized value of length=”+a2.length);

            }

            }

   Method overloading
            Method overloading is one of the efficient & nice way to create methods with same  
         name,  but different lists & different definitions.
                        Note that while declaring overloaded function the parameter of each function  
       have to be different & return type of the method does-not play any part in this.
                        E.g=> program for function overloading proto-type
                       
                        Class a1{
                        float length, breadth;
                        a1 (float x, float y)                                           //constructor 1
                        {
                                    length = x;
                                    breadth =y;
                        }

                        a1 (float x)                                                       //constructor 2
                        {
                                    length = breadth =x;
                        }
                        }


No comments:

Post a Comment