Wednesday, February 12, 2020

Actions, Keys, Window Handle, Drag, Drop

@Test

public void performMouseActions()

{

Actions actions=new Actions(driver);

actions.moveToElement(driver.findElement(By.xpath("//a[@href='https://contribute.jquery.org/']"))).build().perform();

}

}



@Test

public void shiftKeyActiononSearchBox() {

WebElement searchbox=driver.findElement(By.xpath("//input[@name=\"s\"]"));

//Actions action=new Actions(driver);

searchbox.sendKeys(Keys.SHIFT,"textEnter");

}

}


@Test

public  void actionOnDrop() throws InterruptedException {

WebElement droppableclick=driver.findElement(By.xpath("//a[@href=\"https://jqueryui.com/droppable/\"]"));

droppableclick.click();

driver.switchTo().frame(0);

WebElement draggable= driver.findElement(By.xpath("//html[@lang=\"en\"]/descendant::div[@id='draggable']")); 

WebElement droppable= driver.findElement(By.xpath("//body/div[1][@id=\"draggable\"]//following-sibling::div[@id='droppable']"));

Actions action=new Actions(driver);

action.clickAndHold(draggable).moveToElement(droppable).release(draggable).build().perform();

//action.dragAndDrop(draggable, droppable).build().perform();

}

}

@Test

public void testSuccessHandleWindow() {

driver.findElement(By.linkText("Click Here")).click();

String parentWindow=driver.getWindowHandle();

System.out.println("Parent Window: "+parentWindow);

Set<String>allwin=driver.getWindowHandles();

System.out.println("all window in current browser" +allwin);

Iterator<String> it= allwin.iterator();

String childwin=it.next();

driver.switchTo().window(childwin);

driver.switchTo().window(parentWindow);

}

}

Tuesday, February 11, 2020

Constructors in Java

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. 

Rules for creating Java constructor

There are two rules defined for the constructor.
  1. Constructor name must be the same as its class name
  2. A Constructor must have no explicit return type
  3. A Java constructor cannot be abstract, static, final, and synchronized

Note: We can use access modifiers while declaring a constructor. It controls the object creation. In other words, we can have private, protected, public or default constructor in Java.

Types of Java constructors

There are two types of constructors in Java:
  1. Default constructor (no-arg constructor)
  2. Parameterized constructor

Java Default Constructor

A constructor is called "Default Constructor" when it doesn't have any parameter.

example

  1. //Java Program to create and call a default constructor  
  2. class Bike1{  
  3. //creating a default constructor  
  4. Bike1(){System.out.println("Bike is created");}  
  5. //main method  
  6. public static void main(String args[]){  
  7. //calling a default constructor  
  8. Bike1 b=new Bike1();  
  9. }  
  10. }  

O/P : Bike is created

Rule: If there is no constructor in a class, compiler automatically creates a default constructor.

Q) What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

Example of default constructor that displays the default values

  1. //Let us see another example of default constructor  
  2. //which displays the default values  
  3. class Student3{  
  4. int id;  
  5. String name;  
  6. //method to display the value of id and name  
  7. void display(){System.out.println(id+" "+name);}  
  8.   
  9. public static void main(String args[]){  
  10. //creating objects  
  11. Student3 s1=new Student3();  
  12. Student3 s2=new Student3();  
  13. //displaying values of the object  
  14. s1.display();  
  15. s2.display();  
  16. }  
  17. }  

O/P : 
0 null
0 null

Explanation:In the above class,you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.

Java Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
example
  1. //Java Program to demonstrate the use of the parameterized constructor.  
  2. class Student4{  
  3.     int id;  
  4.     String name;  
  5.     //creating a parameterized constructor  
  6.     Student4(int i,String n){  
  7.     id = i;  
  8.     name = n;  
  9.     }  
  10.     //method to display the values  
  11.     void display(){System.out.println(id+" "+name);}  
  12.    
  13.     public static void main(String args[]){  
  14.     //creating objects and passing values  
  15.     Student4 s1 = new Student4(111,"Karan");  
  16.     Student4 s2 = new Student4(222,"Aryan");  
  17.     //calling method to display the values of object  
  18.     s1.display();  
  19.     s2.display();  
  20.    }  
  21. }  
O/P
111 Karan
222 Aryan

Constructor Overloading in Java

In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.

Example of Constructor Overloading

  1. //Java program to overload constructors  
  2. class Student5{  
  3.     int id;  
  4.     String name;  
  5.     int age;  
  6.     //creating two arg constructor  
  7.     Student5(int i,String n){  
  8.     id = i;  
  9.     name = n;  
  10.     }  
  11.     //creating three arg constructor  
  12.     Student5(int i,String n,int a){  
  13.     id = i;  
  14.     name = n;  
  15.     age=a;  
  16.     }  
  17.     void display(){System.out.println(id+" "+name+" "+age);}  
  18.    
  19.     public static void main(String args[]){  
  20.     Student5 s1 = new Student5(111,"Karan");  
  21.     Student5 s2 = new Student5(222,"Aryan",25);  
  22.     s1.display();  
  23.     s2.display();  
  24.    }  
  25. }  
O/P
111 Karan 0
222 Aryan 25


Difference between constructor and method in Java

There are many differences between constructors and methods. They are given below.
Java ConstructorJava Method
A constructor is used to initialize the state of an object.A method is used to expose the behavior of an object.
A constructor must not have a return type.A method must have a return type.
The constructor is invoked implicitly.The method is invoked explicitly.
The Java compiler provides a default constructor if you don't have any constructor in a class.The method is not provided by the compiler in any case.
The constructor name must be same as the class name.The method name may or may not be same as the class name.




Monday, February 10, 2020

OOPS


OOPS Concept:
Java OOPs Concepts

Object

Any entity that has state and behaviour is known as an object.

 For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

An Object can be defined as an instance of a class

An object has three characteristics:
  • State: represents the data (value) of an object.
  • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
  • Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.

Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.

Class

Collection of objects is called class. It is a logical entity.

A class can also be defined as a blueprint from which you can create an individual object.

Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.


Why use inheritance in java


The syntax of Java Inheritance

  1. class Subclass-name extends Superclass-name  
  2. {  
  3.    //methods and fields  
  4. }  


The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.

In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.

Java Inheritance Example

Inheritance in Java
As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.

  1. class Employee{  
  2.  float salary=40000;  
  3. }  
  4. class Programmer extends Employee{  
  5.  int bonus=10000;  
  6.  public static void main(String args[]){  
  7.    Programmer p=new Programmer();  
  8.    System.out.println("Programmer salary is:"+p.salary);  
  9.    System.out.println("Bonus of Programmer is:"+p.bonus);  
  10. }  
  11. }  
O/P
Programmer salary is:40000.0
 Bonus of programmer is:10000

In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability.
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
Types of inheritance in Java

Note: Multiple inheritance is not supported in Java through class.

When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Multiple inheritance in JavaSingle Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

File: TestInheritance.java
Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.

In Java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.

  1. class Animal{  
  2. void eat(){System.out.println("eating...");}  
  3. }  
  4. class Dog extends Animal{  
  5. void bark(){System.out.println("barking...");}  
  6. }  
  7. class TestInheritance{  
  8. public static void main(String args[]){  
  9. Dog d=new Dog();  
  10. d.bark();  
  11. d.eat();  
  12. }}  
O/P
barking...
eating...

Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.
  1. class Animal{  
  2. void eat(){System.out.println("eating...");}  
  3. }  
  4. class Dog extends Animal{  
  5. void bark(){System.out.println("barking...");}  
  6. }  
  7. class BabyDog extends Dog{  
  8. void weep(){System.out.println("weeping...");}  
  9. }  
  10. class TestInheritance2{  
  11. public static void main(String args[]){  
  12. BabyDog d=new BabyDog();  
  13. d.weep();  
  14. d.bark();  
  15. d.eat();  
  16. }}  
Output:
weeping...
barking...
eating...

Hierarchical Inheritance Example

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.
  1. class Animal{  
  2. void eat(){System.out.println("eating...");}  
  3. }  
  4. class Dog extends Animal{  
  5. void bark(){System.out.println("barking...");}  
  6. }  
  7. class Cat extends Animal{  
  8. void meow(){System.out.println("meowing...");}  
  9. }  
  10. class TestInheritance3{  
  11. public static void main(String args[]){  
  12. Cat c=new Cat();  
  13. c.meow();  
  14. c.eat();  
  15. //c.bark();//C.T.Error  
  16. }}  
O/P
meowing...
eating...

Q) Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.
  1. class A{  
  2. void msg(){System.out.println("Hello");}  
  3. }  
  4. class B{  
  5. void msg(){System.out.println("Welcome");}  
  6. }  
  7. class C extends A,B{//suppose if it were  
  8.    
  9.  public static void main(String args[]){  
  10.    C obj=new C();  
  11.    obj.msg();//Now which msg() method would be invoked?  
  12. }  
  13. }  
O/P
Compile Time Error

Aggregation in Java

If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.
  1. class Employee{  
  2. int id;  
  3. String name;  
  4. Address address;//Address is a class  
  5. ...  
  6. }  
In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.

Why use Aggregation?

  • For Code Reusability.

Simple Example of Aggregation

aggregation example
In this example, we have created the reference of Operation class in the Circle class.
  1. class Operation{  
  2.  int square(int n){  
  3.   return n*n;  
  4.  }  
  5. }  
  6.   
  7. class Circle{  
  8.  Operation op;//aggregation  
  9.  double pi=3.14;  
  10.     
  11.  double area(int radius){  
  12.    op=new Operation();  
  13.    int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).  
  14.    return pi*rsquare;  
  15.  }  
  16.  public static void main(String args[]){  
  17.    Circle c=new Circle();  
  18.    double result=c.area(5);  
  19.    System.out.println(result);  
  20.  }  
  21. }  
Output:78.5

When use Aggregation?

  • Code reuse is also best achieved by aggregation when there is no is-a relationship.
  • Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.

Understanding meaningful example of Aggregation

In this example, Employee has an object of Address, address object contains its own informations such as city, state, country etc. In such case relationship is Employee HAS-A address.

Address.java

  1. public class Address {  
  2. String city,state,country;  
  3.   
  4. public Address(String city, String state, String country) {  
  5.     this.city = city;  
  6.     this.state = state;  
  7.     this.country = country;  
  8. }  
  9. }  

Emp.java

  1. public class Emp {  
  2. int id;  
  3. String name;  
  4. Address address;  
  5.   
  6. public Emp(int id, String name,Address address) {  
  7.     this.id = id;  
  8.     this.name = name;  
  9.     this.address=address;  
  10. }  
  11.   
  12. void display(){  
  13. System.out.println(id+" "+name);  
  14. System.out.println(address.city+" "+address.state+" "+address.country);  
  15. }  
  16.   
  17. public static void main(String[] args) {  
  18. Address address1=new Address("gzb","UP","india");  
  19. Address address2=new Address("gno","UP","india");  
  20.   
  21. Emp e=new Emp(111,"varun",address1);  
  22. Emp e2=new Emp(112,"arun",address2);  
  23.       
  24. e.display();  
  25. e2.display();  
  26.       
  27. }  
  28. }  
Output:111 varun
       gzb UP india
       112 arun
       gno UP india    

 

Abstract class in Java

Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing.
In Java, we use abstract class and interface to achieve abstraction.

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java
  1. Abstract class (0 to 100%)
  2. Interface (100%)

Abstract class in Java

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Points to Remember

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.
Rules for Java Abstract class

Example of abstract class

abstract class A{}  

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an abstract method.

Example of abstract method

abstract void printStatus();//no method body and abstract  

Example of Abstract class that has an abstract method

  1. abstract class Bike{  
  2.   abstract void run();  
  3. }  
  4. class Honda4 extends Bike{  
  5. void run(){System.out.println("running safely");}  
  6. public static void main(String args[]){  
  7.  Bike obj = new Honda4();  
  8.  obj.run();  
  9. }  
  10. }  
O/P:
running safely

Understanding the real scenario of Abstract class

  1. abstract class Shape{  
  2. abstract void draw();  
  3. }  
  4. //In real scenario, implementation is provided by others i.e. unknown by end user  
  5. class Rectangle extends Shape{  
  6. void draw(){System.out.println("drawing rectangle");}  
  7. }  
  8. class Circle1 extends Shape{  
  9. void draw(){System.out.println("drawing circle");}  
  10. }  
  11. //In real scenario, method is called by programmer or user  
  12. class TestAbstraction1{  
  13. public static void main(String args[]){  
  14. Shape s=new Circle1();//In a real scenario, object is provided through method, e.g., getShape() method  
  15. s.draw();  
  16. }  
  17. }  
drawing circle

Another example of Abstract class in java

  1. abstract class Bank{    
  2. abstract int getRateOfInterest();    
  3. }    
  4. class SBI extends Bank{    
  5. int getRateOfInterest(){return 7;}    
  6. }    
  7. class PNB extends Bank{    
  8. int getRateOfInterest(){return 8;}    
  9. }    
  10.     
  11. class TestBank{    
  12. public static void main(String args[]){    
  13. Bank b;  
  14. b=new SBI();  
  15. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
  16. b=new PNB();  
  17. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
  18. }}    
Rate of Interest is: 7 %
Rate of Interest is: 8 %

Abstract class having constructor, data member and methods

An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
  1. //Example of an abstract class that has abstract and non-abstract methods  
  2.  abstract class Bike{  
  3.    Bike(){System.out.println("bike is created");}  
  4.    abstract void run();  
  5.    void changeGear(){System.out.println("gear changed");}  
  6.  }  
  7. //Creating a Child class which inherits Abstract class  
  8.  class Honda extends Bike{  
  9.  void run(){System.out.println("running safely..");}  
  10.  }  
  11. //Creating a Test class which calls abstract and non-abstract methods  
  12.  class TestAbstraction2{  
  13.  public static void main(String args[]){  
  14.   Bike obj = new Honda();  
  15.   obj.run();  
  16.   obj.changeGear();  
  17.  }  
  18. }  
 bike is created
       running safely..
       gear changed

Rule: If there is an abstract method in a class, that class must be abstract.

  1. class Bike12{  
  2. abstract void run();  
  3. }  
compile time error

Rule: If you are extending an abstract class that has an abstract method, you must either provide the implementation of the method or make this class abstract.

Another real scenario of abstract class

The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface.
  1. interface A{  
  2. void a();  
  3. void b();  
  4. void c();  
  5. void d();  
  6. }  
  7.   
  8. abstract class B implements A{  
  9. public void c(){System.out.println("I am c");}  
  10. }  
  11.   
  12. class M extends B{  
  13. public void a(){System.out.println("I am a");}  
  14. public void b(){System.out.println("I am b");}  
  15. public void d(){System.out.println("I am d");}  
  16. }  
  17.   
  18. class Test5{  
  19. public static void main(String args[]){  
  20. A a=new M();  
  21. a.a();  
  22. a.b();  
  23. a.c();  
  24. a.d();  
  25. }}  
Output:I am a
       I am b
       I am c
       I am d

Encapsulation

Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.
encapsulation in java
We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
The Java Bean class is the example of a fully encapsulated class.


By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
It provides you the control over the data
It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.

The encapsulate class is easy to test. So, it is better for unit testing.

The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.
Simple Example of Encapsulation in Java
  1. //A Java class which is a fully encapsulated class.  
  2. //It has a private data member and getter and setter methods.  
  3. package com.javatpoint;  
  4. public class Student{  
  5. //private data member  
  6. private String name;  
  7. //getter method for name  
  8. public String getName(){  
  9. return name;  
  10. }  
  11. //setter method for name  
  12. public void setName(String name){  
  13. this.name=name  
  14. }  
  15. }  

File: Test.java
  1. //A Java class to test the encapsulated class.  
  2. package com.javatpoint;  
  3. class Test{  
  4. public static void main(String[] args){  
  5. //creating instance of the encapsulated class  
  6. Student s=new Student();  
  7. //setting value in the name member  
  8. s.setName("vijay");  
  9. //getting value of the name member  
  10. System.out.println(s.getName());  
  11. }  
  12. }  
Output: vijay


Polymorphism in Java

Polymorphism in Java is a concept by which we can perform a single action in different wayspolymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

Runtime Polymorphism in Java

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
Let's first understand the upcasting before Runtime Polymorphism.

Upcasting

If the reference variable of Parent class refers to the object of Child class, it is known as upcasting. For example:
Upcasting in Java
  1. class A{}  
  2. class B extends A{}  

A a=new B();//upcasting  

For upcasting, we can use the reference variable of class type or an interface type. For Example:

  1. interface I{}  
  2. class A{}  
  3. class B extends A implements I{}  


Here, the relationship of B class would be:
B IS-A A
B IS-A I
B IS-A Object

Since Object is the root class of all classes in Java, so we can write B IS-A Object.


Example of Java Runtime Polymorphism

In this example, we are creating two classes Bike and Splendor. Splendor class extends Bike class and overrides its run() method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, the subclass method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is known as runtime polymorphism.


  1. class Bike{  
  2.   void run(){System.out.println("running");}  
  3. }  
  4. class Splendor extends Bike{  
  5.   void run(){System.out.println("running safely with 60km");}  
  6.   
  7.   public static void main(String args[]){  
  8.     Bike b = new Splendor();//upcasting  
  9.     b.run();  
  10.   }  
  11. }  


Output: running safely with 60km.

Java Runtime Polymorphism Example: Bank

Consider a scenario where Bank is a class that provides a method to get the rate of interest. However, the rate of interest may differ according to banks. For example, SBI, ICICI, and AXIS banks are providing 8.4%, 7.3%, and 9.7% rate of interest.
Java Runtime Polymorphism example of bank



  1. class Bank{  
  2. float getRateOfInterest(){return 0;}  
  3. }  
  4. class SBI extends Bank{  
  5. float getRateOfInterest(){return 8.4f;}  
  6. }  
  7. class ICICI extends Bank{  
  8. float getRateOfInterest(){return 7.3f;}  
  9. }  
  10. class AXIS extends Bank{  
  11. float getRateOfInterest(){return 9.7f;}  
  12. }  
  13. class TestPolymorphism{  
  14. public static void main(String args[]){  
  15. Bank b;  
  16. b=new SBI();  
  17. System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());  
  18. b=new ICICI();  
  19. System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());  
  20. b=new AXIS();  
  21. System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());  
  22. }  
  23. }  
Output:
SBI Rate of Interest: 8.4
ICICI Rate of Interest: 7.3
AXIS Rate of Interest: 9.7