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..:)




No comments:

Post a Comment