Selenium and Java

Friday, 26 June 2015

Java- Throw, Throws, Throwable

Java- Throw, Throws, Throwable

Throw:

throw is a keyword in java which is used to throw an exception manually. Using throw keyword, you can throw an exception from any method or block. But, that exception must be of type java.lang.Throwable class or it’s sub classes. Below example shows how to throw an exception using throw keyword.

class ThrowAndThrowsExample
{
 void method() throws Exception
 {
  Exception e = new Exception();

  throw e;            //throwing an exception using 'throw'
 }
}

Thursday, 25 June 2015

Java- Final , Finally and Finalize

Java- Final , Finally and Finalize

Final:

Final is a keyword. The variable decleared as final should be initialized only once and cannot be changed. Java classes declared as final cannot be extended. Methods declared as final cannot be overridden.Final is a keyword.

final can be used to mark a variable "unchangeable"

private final String name = "foo";  //the reference name can never change

final can also make a method not "overrideable"

public final String toString() {  return "NULL"; }

final can also make a class not "inheritable". i.e. the class can not be subclassed.

public final class finalClass {...}
public class classNotAllowed extends finalClass {...} // Not allowed

Finally:

Finally is a block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling - it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Finally is a block.


lock.lock();
try {
  //do stuff
} catch (SomeException se) {
  //handle se
} finally {
  lock.unlock(); //always executed, even if Exception or Error or se
}

Finalize:

Finalize is a method. Before an object is garbage collected, the runtime system calls its finalize() method. You can write system resources release code in finalize() method before getting garbage collected. Finalize is a method.

Wednesday, 24 June 2015

Selenium - Excel creation using APACHE POI in selenium Webdriver framework

Code to create Excel sheet at run time using APACHE POI..


String filepath = "D:\\Mohit1.xls";

FileOutputStream fileout = new FileOutputStream(filepath);

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();

Row row;
Cell cell;
for (int i=0 ; i< 4 ; i++){

row = sheet.createRow(i);
for (int j =0 ; j<3 ;j++){

cell = row.createCell(j);
cell.setCellValue("data"+ i + j);
}
}

workbook.write(fileout);

Selenium - Selenium IDE

Selenium IDE is a Firefox plug in , we can record actions on webpage like type, select , click  and we can play back same recorded script again to verify the action.

Java - Overriding method


Overriding Method :


Method in a subclass with the same signature (name,number and the type of its parameters) and return type as method in the super class overrides the super class's method.

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.


Super keyword in Overriding


super keyword is used for calling the parent class method/constructor. super.methodname() calling the specified method of base class while super() calls the constructor of base class. Let’s see the use of super in Overriding



Static Methods


If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

The distinction between hiding a static method and overriding an instance method has important implications:
  1. The version of the overridden instance method that gets invoked is the one in the subclass.
  2. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Advantage of method overriding :

The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class(base class).


Rules of method overriding in Java:

  1. Argument list: The argument list of overriding method must be same as that of the method in parent class. The data types of the arguments and their sequence should be maintained as it is in the overriding method.
  2. Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class. For e.g. if the Access Modifier of base class method is public then the overriding method (child class method ) cannot have private, protected and default Access modifier as all of the three are more restrictive than public.
  3. Private, static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.
  4. Overriding method (method of child class) can throw any unchecked exceptions, regardless of whether the overridden method(method of parent class) throws any exception or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.
  5. If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is a abstract class
Example of Overriding in Java:


public class OverrideBaseClass {

public int add (int a,int b){
int c= a+b;
return c;
}
}
public class OverridingChildClass extends OverrideBaseClass {

public int add (int a,int b){
System.out.println("Addition is " +super.add(5,6));
int c= a+b+5;
return c;
}
public static void main(String[] args) {

OverrideBaseClass overr = new OverridingChildClass();
System.out.println("Addition is " + overr.add(5,6));
}
}



Output: 
Addition is 11
Addition is 16

Java- Static Keyword

Java- Static Keyword


The static keyword can be used in 3 scenarios:

  • static variables
  • static methods
  • static blocks of code

1. Static Variable :

When a variable is declared with the keyword “static”, its called a “class variable”. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance.

Without the “static” keyword, it’s called instance variable, and each instance of the class has its own copy of the variable.

  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object
  • Syntax : <class-name>.<variable-name>
















2. Static Method:

Any method defined as Static can be called without creating an object of class. The best example is main method , as at the start of execution no object created so main method is defined as Static.A
static method belongs to class rather than an object of the class

Calling Static Methods

Invoke static methods using the name of the class followed by dot (.), then the name of the method:
classname.staticMethodName(args,...)

There are two main restrictions for the static method. They are:
  1. The static method can not use non static data member or call non-static method directly.
  2. this and super cannot be used in static context.

Tuesday, 23 June 2015

Java- Encapsulation


Encapsulation :

To define what level of information share with other word in Java is called Encapsulation. Access restriction level is achieved by Access modifiers (Privet, Public, Package, Protected), These are 4 Ps of Object oriented program .Encapsulation in java is a process of wrapping code and data together into a single unit.

Access Modifiers :

Java provides access modifiers to set access levels for classes, variables, methods and constructors. A member has package or default accessibility when no accessibility modifier is specified.
Four access modifier available in Java as Package Privet, Protected, Public.
Package access modifier is default access modifier, that means if we don't use any access modifier then Package level restriction will be applied.
Public Access modifier is the least restrictive where as Privet is most restrictive.
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers.


Modifier Class Package Subclass World
PublicYYYY
ProtectedYYYN
PackageYYNN
PrivateNYNN


1. Public Access Modifier:

Public access modifier is least restrictive and class ,method ,interface and variables with this access modifier are accessible from any class in Java.

However if the public class we are trying to access is in a different package, then the public class still need to be imported.
Because of class inheritance, all public methods and variables of a class are inherited by its sub classes.




2. Package Access Modifier (default):

If we don't use any access modifiers than Default access modifier Package is considered by default. In this case class ,method and variable will be access withing same package. we can not use class, method and variable defined as package or no access modifiers outside package.






3. Protected Access Modifier :

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.
Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.




4. Privet Access Modifier :

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Variables that are declared private can be accessed outside the class if public getter methods are present in the class.
Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world.

Monday, 8 June 2015

Java- Polymorphism

Polymorphism : 

Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object (inheritance).

Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in java: compile time polymorphism (overloading) and runtime polymorphism (overriding). We can perform polymorphism in java by method overloading and method overriding.

public class Animal{} public class Deer extends Animal {}

Overloading Method : 

Overloading is determined at the compile time. It occurs when several methods have same names with:

      a. Different method signature and different number or type of parameters.
      b.Same method signature but different number of parameters.
      c.Same method signature and same number of parameters but of different type

Overriding Method : 

Overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.

Some Interview Question:

1.What is polymorphism and what are the types of it?
Polymorphism in java is a concept by which we can perform a single action by different ways
Method overloading(compile time polymorphism),method overriding(run time polymorphism)

2.What is method overriding?
Specific implementation of a method for child class.

3.What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading

4.What is static and dynamic binding?
static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at run time.

5.can we overload main() method?
Yes,  we can have many main() methods in a class by overloading the main method.

Java- Overloading Method


Overloading Method : 

              If a class have multiple methods by same name but different parameters, it is known as Method Overloading. Method overloading increases the readability of the program. It is also known as Static Polymorphism or compile time Polymorphism. 

Points to considered for overloading when method name is same:

  1. The number of parameters is different for the methods.
  2. The parameter types are different (like changing a parameter that was a float to an int). 
  3. Sequence of Data type of parameters. 
  4. Constructor in Java can be overloaded
  5. Overloading can take place in the same class or in its sub-class.

Points when overloading not considered :

  1. Just changing the return type of the method.  If the return type of the method is the only thing changed, then this will result in a compiler error.  
  2. Changing just the name of the method parameters, but not changing the parameter type.If the name of the method parameter is the only thing changed then this will also result in a compiler error

Example of Overloading : 

As explained earlier overloading done at compile time..
Overloading in same class..


















----------------------------------------------------------------------------------------------------

Overloading in Sub Class...