28 September 2012

Tutorial #8

            Friends let’s start this tutorial from one of the basic technique that we haven’t discussed
    till now , the technique of taking data values from the user to process & writing data to the file 
    at any drive of our system.
            So here it goes

            Inputing data from the user or reading & writing in file
            import java.io.DataInputStream;
            import java.io.FileNotFoundException;
            import java.io.FileOutputStream;
            import java.io.IOException;

                        //creating a text file
            public class FileInput {
                        public static void main(String[] args) throws IOException {
                                    //attach a keyboard to data stream
                                    DataInputStream dis = new DataInputStream(System.in);
                                    //attach file to file output stream
                                    FileOutputStream fout = new FileOutputStream("d:\\myfile.txt",true);
                                    //read data from DataInputStream & write into FileOutputStream;
                                    char ch;
                                    System.out.println("enter data with '@' at the end");
                                    while((ch =(char)dis.read())!='@')
                                                fout.write(ch);
                                    fout.close();
            }

      }

**         Static Member (static method & static variable) in java

            The main advantage of static members is that it can be called without  instantiating the
     corresponding class (i.e=> without creating objects for that class).
            The common way of declaring static members (static method & static variable) is given  
      below-
                        static int count;                                                //static variable
                        static int max(int x , it z);                                 //static method

     The variable that are declared static is associated with the class itself, so it is changed by
     any object also affect its value in other object;

            Restriction imposed on static method-
1.    They can only call other static method;
2.    They can only access static data;
3.    They can-not refer to this or super in any way;

                        E.g=> example explaining  the concept of static method & static data properly.

**         Nesting of method
            A method can be called by its using only its name by another method of the same class.   
           This is known as nesting of methods.
                        E.g=> explaning properly about this concept(program 8.5)

**         Defining subclass
            A subclass is a class that usually inherits (or takes propertoies ) from some other
            prebuild class called as parent class (or superclass);


            syntax
                        class subclassname extends superclassname
                        {
                                    Variable declaration;
                                    Method declaration;
                        }

         The keyword extends signifies that the properties of the superclassname are extended to
         the superclassname.


          Note:- the constructor in the derived class uses super keyword to pass the value to its
          superclass.
                        E.g=> program showing single inheritance; (program 8.6) beauty

**         Features of subclass constructor

            Keyword super is used to subject the following condition-
·                  Super may only be used within a subclass constructor method
·                     The call to superclass constructor must appear as the first statement within the subclass  
            constructor.
·                     The parameter in the super() method for passing parameter to a subclass;
 
**         overriding method

            Defining a method in the subclass with same name & argument as being declared in the
            superclass is called as method overriding. When that method is called , the method
            defined in the subclass is called instead of super class & hence it overrides the
            superclass declared method..:)

                                  
**         Final variable & method
            All the methods can be overridden by default in subclass, if we don’t want to allow the
            subclass to override the member of the superclass we can declare them using “final”
            keyword
                        E.g=> final int length = 98;

                                    final void pension()
                                    {
                                    ----- function body------
                                    }          

            Final Class
            It is the class that can-not be subclassed . This is also being achieved by using the 
            keyword “final”.
                        E.g=> final class reaction {------ }
                                   final class action extends reaction {--------}

            So from the above example it is clear that a final class can act as subclass but it will 
            never act as superclass.

**         Finalizer method      
            This concept is just the opposite of initialization. As we know that java has automatic 
            runtime garbage collector that automatically frees the memory as soon as the program
            finishese execution. But object may holds other non-object resources that is being freed
            by finalize() method like file descriptor or window system fonts.
                                  This is similar to destructor method in C++;
       
            Here is the link to download .java file of the example program given in this tutorial 
            named as FileInput.java.
                 https://www.dropbox.com/s/e6lcl0r5u2zmfsz/FileInput.java

            Just you need to download this file and place it on src of myEclipse & it will get 
            ready to run..:)




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;
                        }
                        }