8 October 2012

Tutorial #9

This tutorial starts with abstract method & classes & will take us through method of putting variable argument
in calling function as well as different types of access specifier present in java  with few example on each topic.
      So lets starts with -

   Abstract Methods & Classes
   We have seen in our earlier tutorial that by using final we ensure that the method is not redefined in a subclass.
    Java also allow us to do something that is exactly opposite to this, i.e=> We can indicate that a method
 must allows be redefined in a subclass, thus making overriding compulsory. This is done using modifier
 keyword "abstract" in the method definition.
                      E.g=> abstract class Ball
                                 {
                                           -------------
                                           -------------
                                           abstract void cricket( );
                                           -------------
                                           -------------
                                  }
 If a class contains more than one abstract method then also it is to be defined in the same way as defined in
 the above example.

  If we are using abstract class we must have to satisfy the following conditions:-
  1. We can-not use abstract classes to instantiate object directly.
  2. The abstract methods of an abstract class must be defined in its subclass.
  3. We can-not declare abstract constructors or abstract static method.

 METHOD WITH VARARGS
 Varargs represent variable length arguments in method , which is one of the feature that is being introduced in J2SE 5.0
         Varargs syntex -
             <access specifier> <static> void method-name (object . . . arguments)
             {
                               -----------normal code (method body)-----------
              }

        NOTE- 1.The varargs must be final argument in the argument list of a method
                     2.Varargs does not generate any compile time error even if an empty argument is passed as a
                          parameter to a method
  Have a look at the example program to understand this concept properly:-
_______________________________________________________________________________
    // Illustrates the use of varargs to print the string value passed as an argument to a method
 class VariableArgs {
    VariableArgs (String... person ){
        for (String name: person){
            System.out.println("Hello"+ name);
        }
    }
   
    public static void main(String[] args) {
    new VariableArgs("John", "Rupali");       

    }
}
//The downloadable link for the above example is given below
  https://www.dropbox.com/s/kandq9kw35xbu5s/dyn_dis.java
______________________________________________________________________________

   Dynamic method dispatch
    It is an important mechanism in Java that is used to implement runtime polymorphism i.e=> In this
    mechanism . method overriding is resolved at runtime instead of compile time.
    
            Have a look at an example to understand this concept properly:-
____________________________________________________________________________
 class Super {
    public void method(){
        System.out.println("method super");   
    }
}

class sub extends Super{
    public void method(){
        System.out.println("Method sub");
    }
}

class dyn_dis{
    public static void main(String args[]){
        Super A= new sub(); //Sub's object refernces assigned Super type references variable
        A.method(); // sub's version of the method will be called
       
        Super B= new Super();
        B.method();  // super method will be called
       
        sub C= new sub();
        C.method();    // sub method will be called
    }
}

//The downloadable link for the above example is given below
     https://www.dropbox.com/s/kandq9kw35xbu5s/dyn_dis.java

____________________________________________________________________________
  Thus in dynamic method dispatch the type of reference variable is irrelevant while choosing a particular
   version of the overridden method for invocation;
                             Instead it solely depends on the type of object being referred by the reference variable.


   VISIBILITY CONTROL
   It mainly specifies the scope of variable & method's visibility outside the class inside the same package or even outside the package in which class is present.

   1. Public Access
     Any variable or method that is declared as public is visible to the entire class in which it is declared as well as visible even outside the class.
        E.g=> public int number;
                   public void sum ( ) {...........}

  2.Friendly access
       It is mainly invoked when non of the access specifier is declared across the variable or method , So it "default access specifier"
       The main difference between "public" & "friendly" access is that public modofier makes the field visible every-where regardless of package while friendly makes it visible only in same package.

  3. Protected Access
        The protected method makes the fields visible not only to all classes & subclasses in the same package
   but also to subclasses of other package

  4. Private Acess
       It enjoys the highest degree of protection where visibilty is only limted to its own class only
            E.g=> private int rome;
                       private int sum( );

  5. private protected aceess
       This gives the visibility level in between  "protected" access & "private" access. This modifier makes the fields visible in all subclasses regardless of package they are in. But they are not accessible by other classes in the same package.

   Rule of thumb:-
   1.Use "public" if the field is to be visible everywhere.
   2.Use "protected" if the field is to be visible everywhere in the crrent package but visible to only
      subclasses of other package.
   3.Use "default" if the field is to be visible everywhere in the current package only.
   4.Use "private protected" if the field is to be visible only in subclasses , regardless of packages.
   5. Use "private" if the field is not to be visible anywhere except its own class.


No comments:

Post a Comment