Automation Using Selenium Webdriver

Thursday 24 November 2016

Top 15 Java abstract class and abstract method interview questions and programs


1.What is abstract class in java?
  • Hiding the implementation  and showing the function definition to the user.
  • Abstract class contains abstract methods and concrete methods(normal methods)
2.How can we define an abstract class? 
  • Using abstract keyword we can define abstract class.
  • Check below code for abstract class example program. 

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   //
  5. }


3. How to declare an abstract method?

  • By Using abstract keyword in the method signature we can declare abstract method.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   abstract void show(); 

  5. }

 4. Can we define abstract class without abstract method?

  •  Yes we can define abstract class without abstract methods.
  • It is not mandatory to create at-least one abstract method in abstract class.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.  

  5. }

 Read more at Can you define an abstract class without any abstract methods? if yes what is the use of it?


 5.Can we create object object for abstract class?
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly.
  • Through sub class abstract class members will get memory. Whenever we create sub class object of abstract class  abstract class object will be created. i.e abstract class members will get memory at that time.


  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractClass obj= new AbstractClass (); 
  13. // ERROR: Cannot instantiate the type AbstractClass
  14.  
  15. }
  16. }

 6. Is is possible to declare abstract method as static?
  • No. its not possible to declare abstract method with static keyword.
  • If we declare abstract method with static compiler will throw an error.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }




7.Can we declare abstract method as final?
  • No. its not possible to declare abstract method with final keyword.
  • If we declare abstract method with final compiler will throw an error.
  • Because abstract methods should be override by its sub classes.


  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }



8. Is it possible to declare abstract method as private?
  • No. its not possible to declare abstract method with private .
  • If we declare abstract method with private compiler will throw an error.
  • Because abstract methods should be override by its sub classes.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     private abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
9. Is it possible to declare abstract method as public ?
  • Yes we can declare abstract methods as public.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     public abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
10. Is it possible to declare abstract method with default?
  • Yes we can declare abstract methods with default.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.    abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

11. Is it possible to declare abstract method with protected modifier?
  • Yes we can declare abstract methods as protected.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     protected abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 

12.What are the valid and invalid keywords or modifier with abstract class?
  • public,  protected and default are valid.
  • static,  final and private are invalid.

13.Can abstract method declaration include throws clause?
  • Yes. We can define an abstract class with throws clause.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     abstract void add() throws Exception; 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
 14.What happens if sub class not overriding abstract methods?
  • If sub class which is extending abstract class not overriding abstract method compiler will throw an error.
  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public class Sample extends AbstractClass {
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }
15. Can we escape of overriding abstract class in sub class which is extending abstract class?
  • Yes we can escape from overriding abstract method from super abstract class by making our class again as abstract.
  • The class which is extending our class will get responsibility of overriding all abstract methods in our class and in super class.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public abstract class Sample extends AbstractClass {
  3.  
  4.    
  5. }

  1. package Abstraction;
  2. public class Example extends Sample{
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }

You Might Like:

Wednesday 23 November 2016

Capture and Navigate All The Links on Webpage Using Selenium Webdriver

In case of Sanity Check we can use this Code to check whether all links are functional or not.
Its so simple to capture links and navigate
public static void navigateToAllLinks() throws InterruptedException {
List<WebElement> linksize = driver.findElements(By.tagName(“a”));
int linksCount = linksize.size();
System.out.println(“Total no of links Available: ” + linksCount);
String[] links = new String[linksCount];
System.out.println(“List of links Available: “);
// print all the links from webpage
for (int i = 0; i < linksCount; i++) {
links[i] = linksize.get(i).getAttribute(“href”);
System.out.println(linksize.get(i).getAttribute(“href”));
}
// navigate to each Link on the webpage
for (int i = 0; i < linksCount; i++) {
driver.navigate().to(links[i]);
System.out.println(driver.getTitle());


}
}

Java program To Count the number of words in a String

Java program To Count the number of words in a String::



  1. package com.javatutorial;
  2.  
  3. public class CountNumberofWords {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7. String s="";
  8. int count=0;
  9.  
  10. Scanner in = new Scanner(System.in);
  11. System.out.println("Please enter a String");
  12.  s=in.nextLine();
  13.  
  14. char ch[]= new char[s.length()];    
  15.  
  16. for(int i=0;i<s.length();i++)
  17. {
  18.  
  19.     ch[i]= s.charAt(i);
  20.  
  21.     if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
  22.         count++;
  23.  
  24. }
  25. System.out.println("Number of words in given String: "+count);
  26.  
  27. }

  28. }




Output:
  1. Please enter a String
  2. Java Tutorial
  3. Number of words in given String: 2

Remove Character from string in java



#1: Java Program to Remove all occurrences of a string  

  1. package com.instanceofjava;
  2.  
  3. class RemoveCharString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Java";
  7.     str = str.replace("a", "");
  8.     System.out.println(str);
  9. }
  10. }
Output:
  1. Jv

#2: Java Program to Replace First occurance of Specific index char in a String



  1. package com.injava;
  2.  
  3. class RemoveCharString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Java";
  7.  
  8. //String result = str.substring(0, index) + str.substring(index+1);
  9.  
  10.   String result = str.substring(0, 1) + str.substring(1+1);
  11.   System.out.println(result);
  12.  
  13. }
  14. }


Output:
  1. Jva



#3: Java Program to Remove all Numbers from a string.



  1. package com.instanceofjava;
  2.  
  3. class RemoveNumberString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance12ofjava143";
  7.   str = str.replaceAll("[0-9]","")
  8.   System.out.println(str);
  9.  
  10. }
  11. }
Output:
  1. Instanceofjava

Tuesday 22 November 2016

Top 13 Java Interview Questions on Static keyword

1.what is static in java?

  • Static is a keyword in java.
  • One of the Important keyword in java.
  • Clear understanding of static keyword is required to build projects.
  • We have static variables, static methods , static blocks. 
  • Static means class level.

2.Why we use static keyword in java?

  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.

3.What is static variable in java?

  • Variables declared with static keyword is known as static variables.
  • Static variables gets memory on class loading.
  • Static variables are class level.
  • If we change any static variable value using a particular object then its value changed for all objects means it is common to every object of that class.
  • static int a,b;

  1. package com.instanceofjavastatic;
  2. class StaticDemo{
  3.  
  4. static int a=40;
  5. static int b=60;
  6.  
  7. }


  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.
  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression
  10. }
  11. }

4. what is static method in java with example

  • Method which is having static in its method definition is known as static method.

  1. static void show(){
  2.  
  3. }
  • JVM will not call these static methods automatically. Develioper needs to call these static methods from main method or static block or variable initialization.
  • Only Main method will b called by JVM automatically.
  • We can call these static methods by using class name itself no need to create object.
Read more 

 5.what is static block in java?

  •  Static blocks are the blocks which will have braces and with static keyword.
  • These static blocks will be called when JVM loads the class.
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.

 6.What is the need of static block?

  • Static blocks will be executed at the time of class loading.
  • So if you want any logic that needs to be executed at the time of class loading that logic need to place inside the static block so that it will be executed at the time of class loading.

7.Why main method is static in java?
  • To execute main method without creating object then the main method should be static so that JVM will call main method by using class name itself.

8.Can we overload static methods in java?

  • Yes we can overload static methods in java.
Read more  

9.Can we override static methods in java?

  • NO we can not override static methods in java.
Read more  

10.Can we write static public void main(String [] args)?


  • Yes we can define main method like static public void main(String[] args){}
  •  Order of modifiers we can change.

  1. package instanceofjava;
  2. public MainDemo{
  3. static public void main(String [] args){
  4.  
  5. }
  6. }
11.Can we call super class static methods from sub class?
  • Yes we can call super class static methods from sub class.
  • Read more 

TestNG XML example to execute Multiple Classes

TestNG XML example to execute Multiple Classes

In testng.xml file we can specify multiple name (s) which needs to be executed.

In a project there may be many classes, but we want to execute only the selected classes.

We can pass class names of multiple packages also. If say suppose, we want to execute two classes
in one package and other class from some other package.

The below is the example testng.xml which will execute the class names that are specified.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <classes>
      <class name="com.first.example.Demo1"/>
      <class name="com.first.example.Demo2"/>
      <class name="com.second.example.Demo3"/>
    </classes>
 </test>
</suite>
We need to specify the class names along with packages in between the classes tags.

In the above xml, we have specified class name as “com.first.example.” and “com.first.example.demoOne”
 which are in “com.first.example” package. And class name demoThree is in package “com.second.example.”

All the classes specified in the xml will get executes which have TestNG annotations.

Below are the two example classes under “com.first.example” package which is executed.

Package: “com.first.example”
Classname: Demo1
package com.first.example;

import org.testng.annotations.Test;

public class Demo1 {

@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoOne Class");
}

@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoOne Class");
}
}
Classname: Demo2
package com.first.example;

import org.testng.annotations.Test;

public class Demo2 {

@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoTwo Class");
}

@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoTwo Class");
}
}
Package: “com.second.example”
Classname: Demo3
package com.second.example;

import org.testng.annotations.Test;

public class Demo3 {

@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoThree Class");
}
@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoThree Class");
}
}
We need to run the testng.xml file. (Right click on testng.xml and select Run as ‘TestNG Suite”)
The below is the output for the above example code.