Monday, 22 October 2018

OOPS Concepts for Selenium - Examples to Practice


Java concepts-3 {oops concept}
Java program structure:
-----------------------
1. Class with main method:
Syntax:

package package_name;
public class class_name
  {
//declarations
pubic static void main (String[] args)
{
--------------
--------------//statements
}
  }

Under main method, script will execute that has written under main method only

2. Class with multiple methods:

In general, within a Class we can create multiple methods/functions

Advantages:
-reusability of the script within the class or another class
-script is easy to understand
-easy to idenitfy any logical errors

There are 2 types of methods/functions in a class
a. Static method:
b. Non-static method:

a. Static methods:
When we create methods with "static" keyword, we can call those methods
directly into main method using name of the method (i.e. static method)

Syntax:{class with multiple static methods}

package package_name;
public class class_name{
//declarations
public static void methodOne(){
---------
---------//statements
}
public static void methodTwo(){
----------
----------//statements
}

public static void main (String[] args){
//method calling
}
}


Exp: Write a program to perform different arithmetic operations using static methods in a class


package oops.concepts;

public class ArithemeticEx {
public static int result; //declare class level variable to use it throughout the class
//creating a method to reuse in main method
public static void Addone(){
int a= 10;
int b= 20;
result= a+b;
System.out.println("Addition of given values is: "+result);
}

//creating a method to reuse in main method
public static void multiTwo(){
int x= 10;
int y= 20;
result= x*y;
System.out.println("multiplication of given values is: "+result);
}

public static void main(String[] args) {
//calling methods created above in main method
Addone();
multiTwo();

}

}


2. Non-static methods:
Currently, this method is widely used

Syntax:{to create Non-static methods}
public void methodName(){
------
------} Script
}

To call a non-static method into main method, we need to create instance object
for the class

Syntax: {to create Instance object for Class}
class_name obj= new class_name();
//to call non-static methods
obj.method_name();

NOTE:  A static method belongs to the class and a non-static method belongs to an object

Static methods are useful if you have only one instance where you're going
to use the method, and you don't need multiple copies (objects).

Non-static methods are used if you're going to use your method to create
multiple copies


Exp: Write program to perform different arithmetic operations using non-static methods

package oops.concepts;

public class ArithemeticEx {
public int result;

public void Addone(){
int a= 10;
int b= 20;
result= a+b;
System.out.println("Addition of given values is: "+result);
}

public void multiTwo(){
int x= 10;
int y= 20;
result= x*y;
System.out.println("multiplication of given values is: "+result);
}

public static void main(String[] args) {
//create instance object for class
ArithemeticEx AE= new ArithemeticEx();

//to call sub methods
AE.Addone();
AE.multiTwo();

}

}
---------------------------------------------------------------------------------------------------
Difference between non-static and static methods

Non-static method Static method
1.Memory is allocated as many time as method Memory is allocated only once at the time of class loading.
is called

2. It is specific to an object, these These are common to every object, it is also known
are also known as instance method. as member method or class method.

3.These methods always access with object reference This property always access with class reference
Syntax: Syntax:
Objref.methodname(); className.methodname();

4.if any method wants to be execute multiple times If any method wants to be execute only once
that can be declare as non static. in the program that can be declare as static

-------------------------------
Passing values to submethods:
-------------------------------
in general we should not maintain any fixed values/hard coded
values in sub methods
whereas we pass the values/data to the submethods
while calling those methods from main method
to read values from main method, sub method should have arguments
syntax:
public void methodName(arg1, arg2)
{
-------
-------//statements
}

Exp: Create class with submethod to pass the values from main method


package oops.concepts;

public class ArithemeticEx {
public int result;

public void Addone(int a, int b){

result= a+b;
System.out.println("Addition of given values is: "+result);
}

public void multiTwo(int x, int y){

result= x*y;
System.out.println("multiplication of given values is: "+result);
}

public static void main(String[] args) {
//create instance object for class
ArithemeticEx AE= new ArithemeticEx();

//to call sub methods
AE.Addone(5, 6);
AE.multiTwo(9, 5);

}

}


Exp: Write a program to click on different language links in Google home page
using non-static method

package oops.concepts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleLang {
public WebDriver driver;
public void setUp(){
driver= new FirefoxDriver();
driver.get("http://google.co.in");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

public void langClick(String myLang) throws InterruptedException{
driver.findElement(By.linkText(myLang)).click();
Thread.sleep(3000);
}

public void tearDown(){
driver.quit();
}
public static void main(String[] args) throws InterruptedException {
GoogleLang gl=new GoogleLang();
gl.setUp();
gl.langClick("??????");
gl.langClick("?????");
gl.langClick("English");
gl.tearDown();


}

}

----------------------------
Return value by submethod:
----------------------------
We can use "return" keyword in sub method and specify data type in place of "void"

Syntax:
public data_type subMethod(){
----
----//statements
return value;
}

Exp: Write program to perfrom arithemetic operation, to call submethod we need to pass the test data,
and also submethod should return the value to main method

package oops.concepts;

public class ArithemeticEx {
public int result;

public int Addone(int a, int b){

result= a+b;
return result;
}

public int multiTwo(int x, int y){

result= x*y;
return result;
}

public static void main(String[] args) {
//create instance object for class
ArithemeticEx AE= new ArithemeticEx();

//to call sub methods
int m=AE.Addone(5, 6);
int n=AE.multiTwo(9, 5);
System.out.println("addition of 5 and 6 is: "+m);
System.out.println("multiplication of 9 with 5 is: "+n);

}

}
--------------------
Method overloading:
--------------------

When we have more than one method with same name within the class is called Method overloading

It can differ based on:
-number of arguments in a method
-Data type of arguments
-sequence of data type of arguments

Exp:
public class MethodOL {
public void demoOne(int a, int b){
int c= a*b;
     System.out.println("multiplication of given values is: "+c);
}

public void demoOne(String x, String y){
String z= x+y;
System.out.println(z);
}

public static void main(String[] args) {
MethodOL ml= new MethodOL();
ml.demoOne(5, 6);
ml.demoOne("InsightQ", "Technologies");

}

}

-------------
Constructor:
-------------

Constructor will automatically execute whenever we created instance object for Class

rules:
-constructor name is same as Class name
-should not use "void" in Constructor

NOTE:  In general Constructors are used to initialize values into variables

Exp:

public class ConstEx {

public ConstEx(){
System.out.println("this is sample script");
}

public static void main(String[] args) {

ConstEx ce= new ConstEx();

}

}

--------------------------
Constructor Overloading:
--------------------------

A class having more than one Constructor is called Constructor overloading

Constructors can be differ based on argument list

Exp:
public class ConstEx {

public ConstEx(int a, int b){
int c= a*b;
      System.out.println("multiplication of given values is: "+c);
}
public ConstEx(String x, String y){
String z= x+y;
System.out.println(z);
}

public static void main(String[] args) {

ConstEx ce= new ConstEx(5,6);
ConstEx cex= new ConstEx("Mindq","Systems");
}
}

--------------
Inheritance:
--------------

Acquiring properties from one class to another class is called Inheritance

Parent class:
Parent class, also called as base class/super class.
The class methods that are called into another class is called parent class

Child class:
The class that acquires methods from another class is called
  Child class/derived class/extened class

extends Keyword:
extends is the keyword used to inherit the properties of other parent class
into child class

Syntax:
public class ChildClass extends ParentClass{

}

Exp:

Step 1: Create parent class {i.e. ParentClass} with 2 submethods and without
main method (i.e. non-executable class)

Script:
package oops.concepts;

public class ParentClass {

public void login(){
System.out.println("setup program");
}

public void tearDown(){
System.out.println("it is logout program");
}

}

Step 2: Create child class with "extends" keyword

extends Keyword:  extends is the keyword used to inherit the properties of other class


Exp:

Script:
package oops.concepts;

public class ChildClass extends ParentClass{

public static void main(String[] args) {
ChildClass cc= new ChildClass();
cc.login();
cc.tearDown();
}

}


--------------------
Method overriding:
--------------------

Means same method having in Parent class as well as childclass

Exp: 
package oops.concepts;

public class ChildClass extends ParentClass{
public void login(){
System.out.println("setup program2");
}

public static void main(String[] args) {
ChildClass cc= new ChildClass();
cc.login();
cc.tearDown();
ParentClass pp= new ParentClass();
pp.login();

}

}

------------------------------
Super keyword in Overriding:
------------------------------

Super keyword is used for calling the parent class method/constructor in child class

Syntax: super.methodname() will call the specified method of base class
and super() calls the constructor of base class

Exp:
public class MyChildClass extends MyBaseClass{

public void setUp(){
System.out.println("Login with Invalid data");
super.setUp();
}

public static void main(String[] args) {
MyChildClass mcc= new MyChildClass();
mcc.setUp();
mcc.tearDown();

MyBaseClass mbc= new MyBaseClass();
mbc.setUp();
}
}

No comments:

Post a Comment