Monday, 7 October 2013

Static method and variables

This is one of the very important topics of core Java. In this post i am going to explain you about static in Java. First and very important point is you can access static methods and variables without creating an object of class. You can access static variables and methods by using classname.static_member.

Ex.
  class StatEx
  {
    static int i;
    void fun()
   {
      System.out.println(StatEx.i);
   }
  }

In above example you can notice that i have accessed i variable by using className (StatEx.i). And same you can do with the static member function.

Some of the important features of static ;
  • Static members can be accessed without creating an object of class.
  • Static function can access only static methods/variables.
  • You can initialize static members in static block .It gets executed only once when class loads.
  • Static variables also called as class variable.
  • Outer class or main class can not be static but you can declare inner class as static.
  • Static method can not be overridden.
  • Binding happens at compile time.
  • In java we have one public static void main method which is also static, an entry point for execution.




Sunday, 6 October 2013

Why main method is static?

This is one of the very popular interview question for beginner. So main method is static because without creating an object you can access this method and start the execution. JVM calls this method by using  its class name dot main method(MyClass.main(params). Now think we don't have main method ,so to do the access we have to create an object,to create an object we have to execute certain required line of code.So without static method entry of an execution is not possible that's why main method is static.

Why String in immutable?

String class immutable. You can not change its state once after creation.  String str=new String("java"); This line creates object of string "java" . You have again this time str=new String("core"); So be clear that it does not assign this value to the previous object(having "java" value) instead it creates new object and assigns "java" value. Now str will point only new object which we have created recently having "java" value. An object having "core" value is unreachable , it may get garbage collected when gc will run. This is the reason string is called immutable.

Following could be the reason of string immutability :


  • Mostly we use string value  parameter to pass to another method.
  • Classloader uses string type to load the classes.
  • String could cache its own hash code.
These are the reasons which i think , if you know any other please post here

String in Java

You may have used String many time while learning core java and doing some java programms.

So some time you might thought how string works ?. What is string ?...etc
In this post i would try to explore about string.

What is string ?. String is just a class which stores array of characters. You can create an object of string by using new keyword ,as simple in java we use new keyword  to create an object of class,there is an another way to create  string like create string reference and assign a string value.
            i.e String str="core java"
However the string value ("core java") it stores in string pool. When compile sees str="core java" then it uses string pool to store assigned value.Please note that when you use new keyword , it does not use or refer string pool,instead it creates new object and stores value.

Some important points about string pool :
  • This is an area where JVM uses to store string literals. We call string literal when we just create reference of an string and assigns some string value as we did in last example by assigning "core java" string.
  • Suppose you have done . String s="java"; String s1="Core" . In such scenario JVM creates an object of "java" string in string pool,and in second line we are again creating an object s1 having "java" string value.In such scenario JVM does not create another object just it refers object of s.
 While learning some more important things you might why string is immutable . Mutable means once you created an object you can not change its state.I have explained in detail in why string is mutable



Multithreading in Java

This is one of the very interesting topics in java. Spawning multiple thread to do the parallel tasks.Here you have to take care of resource handling,locking,unlocking otherwise application will buzzed off.

So if we talk about how thread to be implemented? .Definition of thread is the path of execution which shares its memory,resources with the process. Then java provides two ways to implement thread . one is by implementing Runnable interface and second one is by extending Thread class.

Following is the detail of thread implementation :
  • Runnable interface : 
       By implementing runnable interface you can  implement threading.Inside runnable interface you have to override run method to start the thread.
Ex.


public class Threading implements Runnable
{

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Thread t=new Thread(new Threading());
        t.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("In run...");
    }
}


  • Second way is by extending thread class.Code is as below
 public class Threading extends Thread
{

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Threading t=new Threading();
        t.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("In run...");
    }

}


In above two implementation you may have noted that after creating an object we are calling start method and run method gets executed and to start the thread you must have to call start method on thread instead of run.If you call run method then thread will not be created. So the point is when you call start method on thread internally it spawns new thread and executes code inside run method.

Some of the important points about threads are as below:
  • Thread can be implemented by two ways (Runnable interface and Thread class)
  • Normally it is preferred to implement Runnable interface so that you could extend another class at a time.
  • To do the resource handling you have to use synchronization.
  • Thread execution scheduling is depending upon the underlying operating system scheduling scheme.
Abstract class(Abstract class) and interface are opposite side of same coin. You can declare interface by interface interface_name.

Following are the some important points about interface:
  •  Interface contain only method signarute.
  • As like abstract class,interface can not be instantiated.
  • In interface by default member variables are public static final.
  • Interface does not have constructor.
  • One interface can extend multiple interfaces at once. As this is not applicable for classes.
  • To implement a interface we use "implements" keyword.
  • Java contain some empty interfaces like Serialzable, Cloanable...etx. They don't have any method signature.
 Now we will see the difference between abstract class and interface :

Abstract class

This is one of the important and basic features of core Java. You know the class in which you can declare variables,have constructor and member functions,you can create object of an class. As you know in inheritance you can extend one class to another (Inheritance in Java) .

   However abstract class has some different features as following :
  • Abstract class can be declared by preceding abstract keyword to class then class name .i.e abstract class Test { } // this is the abstract class.
  • We can not instantiate abstract class object.
  • Abstract class may inherit another class.
  • Abstract class may contain abstract method (ex. abstract void fun()) which has only signature , it does not have method body.
  • Abstract class may contain non abstract method having body.
  • Abstract class may contain constructor as like normal class.
  • Use of abstract class or interface(Interface) depends upon the scenario where to be used.
  • As like a class it does not allow multiple inheritance.

Final,finally and finalize in java.

This is one of the most famous interview question which is being asked. In this post i will try to explain these three keyword .

Following are the some important points about final in java:
  • If you declare final with variable then that will be considered as constant.You have to initialize constant variable when it is declared or in constructor other wise it throws compile time error.
  • If you use final keyword with method then that method will not be overridden(Overloading and Overriding).
  • If you use final keyword with class then that class can not be extended.
Ex. final int a=12;  //final variable declaration.
      final class Test{ //final class declaration.
      }
    
     class Test
   {
     final void fun() //final method declaration.
     {
     }
   }
    
Few important points about finally :


  • We use finally in context of exception handling.
  • finally is the block of code which it executes either exception is thrown or not.
  • In finally we normally do the task like db connection close,releasing the locked resources...etc
  • In one case finally block will not be executed if you exit from the execution.

Few important points about finalize :
  • finalize is the protected method which is used in context of garbage collection.
  • Signature of this method is protected void finalized() which throws Throwable.
  • This method gets executed before object is getting garbage collected.
  • It gives chance to release allocated resources by an object.

Inheritance in Java

When you start learning ABCD of Java,surely you will see this(Inheritance) keyword . If you come from C++,then you know what is inheritance. However there is some difference between in inheritance in c++ and inheritance in Java. When we proceed through this blog, i will explain .

First of all, inheritance means accessing properties of parent class in child class.Inheritance can be acheived by using extends Class_Name in front of the child class . Ex. class Child extends Parent .

Some important points about inheritance in Java
  • You can only access public ,default,protected member variables and methods in child class.
  • You can not access private members of parent class.
  • Class constructor does not inherits.
  • You can not inherit member variables.
  • You can override (Overriding and overloading)  the methods of parent class in child class.
  • Java does not support multiple inheritance as it leads to diamond problem.
  • You can use reference of parent class and can create object of child class.
I am trying to demonstrate the use of inheritance in simple code :

 public class Inheritance {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Child c=new Child();
        c.printParent();
        c.printChild();
       
    }

}


class Parent
{
    void printParent()
    {
        System.out.println("In parent");
    }
}

class Child extends Parent
{
    void printChild()
    {
        System.out.println("In child");
    }
}

If you think any points are missed the please let me know.

Method overriding and overloading in Java

After going through inheritance now it is time to know about method overriding and overloading.
Method overloading terminology is used where method name is same but it may have different set of parameter list,same parameter with different order. Compile time binding is done for method overloading.

Some important points about method overloading :
  • Method overloading is same method name having different arg list,order withing the class.
  • Binding happens at compile time.
  • Overloaded method must have different sets of param or order or both.
  • Return type in method overloading does not matter as binding happens at compile time.
  • Static method can be overloaded.
Please loot at sample code of overloading :

public class Overload {


    void fun()
    {
      
    }
   
    void fun(int a) //overloaded method of fun
    {
      
    }
}




 Now we will look into method overriding . Method overriding is the concept of overriding the parent method into the child class.
Following are the important points about overriding :


  • Overridden method  has same method signature of parent method.
  • If you mark final method in parent class then that can not be overriden.
  • Binding happens at run time. 
  • One more important point  is if your parent method has throws IOException in signature then in child class overridden method you used "throws Exception", then it shows compile time error .As you can either throw IOException or its child exception.
  • While overriding you can not minimize the visibility of overridden method. It means suppose you have declared public void fun() in your parent class and same method is overridden at child class having void fun() with no visibility mentioned (default) .In such case it throws compile time error.
To be more clear please look at following piece of code:


class Parent
{
    void printParent() throws IOException
    {
        System.out.println("In parent");
    }
}

class Child extends Parent
{
    void printParent() throws IOException
    {
        System.out.println("In child");
    }
}

 

Friday, 4 October 2013

Difference between webdriver and selenium

Now webdriver (selenium 2.0) is the next version of selenium 1.0.  Selenium developers have made some up gradation into webdriver. So i will try to point out those differences as follows ;


  • As selenium injects its core java script into the browser while creating browser profile to drive the operations on browsers like click,type,select ...etc . where as webdriver uses native api of browser to do the operations.
  • Webdriver is pretty faster than selenium 1.0.
  • Webdriver acts like a real user where as selenium is not . Ex. would be suppose you have one alert box and ideally without disappearing that alert box you can not do the operation on web page,such operation can be done by selenium while webdriver does not support it.
  • Webdriver is designed in more object oriented way than selenium.
  • Webdriver supports android and ios app automation.
  • Webdriver supports limited sets of browser , it does not support all the browser which are supported by seleniu 1.0.
Please suggest any point if i have missed here.

Thursday, 3 October 2013

Selenium/Webdriver automation framework using testng.

In this post i will try to explain you about the selenium automation framework using testng.

To build this framework you need following things.
I will explain you the brief explanation of above components in automation framework. This automation framework is the data driver framework because we are keeping data which is input to the automation into mysql database . In later i will explain how the data will driver the execution. We will use basic annotations of testng like @beforetest,@beforesuit,@test ,@aftersuite in framework to do the particular task . @beforetest annoted method gets executed before executing the suite (suite is nothing but a set of test nodes which contains methods,test classes ...),@beforetest annoted methods gets executed before test node of suite ,@aftersuite annoted method gets executed after suite execution. @test annotation will be used to mark method as testng test method , in which we write the actual code for automation. It will support parallel execution of the tests.

To provide the data to the @test method we will use dataprovider from testng. There are two types of data provider one is which returns two dimension object of an array, other one is lazy iterator. So we will use lazy data provider . The difference between these two providers is first one which returns object[][] sends data to the @test annoted method in one pass ,where as lazy data provider sends data one by one , i mean lazy provider sends one set of data to the @test method and waits for its execution completion and sends next set of data. I have implemented this DataProvider as an inner class which implements Iterator<Object[]> interface.

reportng.jar  will be used to generate and html report, This will be used as a third part open source report generator. So use you have to mark this as a listener in your suite xml or in your build.xml

Eclipse  this will be used as an IDE for java development.

Mysql this will be used to store automation related stuffs into the database.

Please find attached sample code and please let me know if you have any concerns.