Understanding important concepts of Java for Selenium – User friendly Tech help

We would be highlighting some of the most vital concepts of Java used in Selenium.Hope it assist our readers to learn and incorporate it while creating the Selenium scripts.

n

    n

  1. Final Keyword
  2. n

  3. Interface in Java
  4. n

  5. Meaning of Static in Java 
  6. n

n

Why we use Final Keyword ?

n

We can use “Final” with a variable, Method or Class
n
nFinal variable :-
nExample Scenario:-
nIn the below code we have declared WebDriver as Final but why??

n

public class LoginPage {
private final WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}

n

nLets understand what is meaning of Final Keyword first!!

n

    n

  • Final Variable, is nothing but a constant(driver would act as constant above), as we cannot change its value once it is initialized.
  • n

  • We need to initialize the value of final variable at the time of declaration,(like final int itest = 10;).We have a exception that we can have final variable uninitialized(blank Final Variable) but  than it must be initialized inside a Constructor (i.e this.driver = driver 😉 the way it is done in our example scenario above.
  • n

n

Now in our example scenario, we have taken webdriver variable as final, so that one’s its value is initialized to the current webdriver instance we don’t want it to change.
n
nFinal Method:-
n1.We cannot override a final method.
nExample:- We have taken TestClassA and TestClassB, where B Extends A, now incase we try to override the method of A in B.
n
n
n
n2.But an subclass can call the final method of parent class but not override.Example we can call the add() method below using TestClassB object

nn

nFinal Class:-
n1.We cannot extend a final Class
nExample:- We have made our TestClassA as Final , and we cannot extend this class in TestClassB.

n

n

nImportant points:-
n1.All variables inside an Interface are by default treated as final.
n2.We need to initialize the final variable value while declaration other option is to initialize it inside a constructor.
n3.We cannot override final method.
n4.We cannot inherit final class.
n5.We cannot declare constructor as final.

Was this article helpful?
YesNo

Similar Posts