Interface in Java- Implementation of Selenium Webdriver interface

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome back to another video on java for selenium in this video will mainly discuss interfaces in Java so we'll discuss three point in this video what is interface how to write interface and how to implement interface so what is again sorry this is some slide issue so what is interface so interface is a special kind of class which contains variable and which cannot be changed so why it cannot be change will get to know into two to three minutes when we write the programs it contains the method declaration only not the methods body so you can only declare the methods you cannot write the body because respect to class has to write the body in technical words it has static constant variables and extracts method only so what is the static constants and abstract method once you write the program you will get to know as of now interface is a special kind of class itself which have only the methods declaration does not contain any body and interface whatever variables we will create you cannot change their values because they are final static constant in nature so how to write you need to write one interface keyword then interface name you can keep the variables and you can keep the methods as well so let me show you through programming but before moving to the program let me draw a diagram then you will get the importance of interface let us say they we have one requirement and this requirement says we need a specific calculator in this calculator you should have minimum addition method subtraction method and let us take multiplication as well we will not write the body we have created a dummy blueprint like this interface has three methods and now you need to write certain classes so let us say we have requirement you need to design an advanced calculator so even though you are creating advanced calculator we know every calculator should have these three methods at least this is the highly recommend requirement another requirement we need to design a scientific calculator scientific calculator can also have multiple methods so let's say scientific calculator may have 300 methods but minimum they should have these three methods they can override it based on the requirement but they have they should have these three methods in the same way you can see advanced calculator can have 500 methods but they should have minimum these three methods and let us say I have one more calculator called ya II my calculator this email calculator have 100 methods but minimum they should have these three methods which is already specified by the interface so you have to notice one thing like using this interface we have created a relationship between all these three classes and we have clearly mentioned they can have their own methods but they should have minimum these three methods like addition subtraction and multiplication they can write the body based on the requirement but they should have these three methods which is already defined by interface we are not writing the body in the interface but this is the requirement which says you need to implement this until you don't implement these methods you cannot create object of your class itself so why it has been introduced in webdriver we will discuss in next video but as of now let us take this development requirement your requirement is you need to create a calculator which can have multiple method but they should have minimum these three methods so let me create a separate package and this package name I will give interface demo here I will create one interface so rightly so till now we have created multiple classes but let us create an interface interface I can give a let us say calculator I will seek LC so this calculator can have only the method declaration method declaration let us say add white subtraction we cannot write the body if you try to write the body here Java will throw error and if you put mouse over here it will say abstract method do not specify a body it means we cannot write the body inside interface it can contain some variable a so let's say if I create one variable say X equal to 10 so you can use this value inside the classes but you cannot change this value so let us say I have one class called advanced calculator and the requirement is highly specific that you have to implement this calculator otherwise this advanced calculator will not be valid since it is a requirement I will write one keyword called implements and then I will say Kelsey so when you say Kelsey it means you are implementing the Kelsey interface which has certain methods so when implements Java will ask you please add the unemployment methods so if I click on this it will create the dummy structure of the methods which is coming from the interface so it's generating two methods and it will ask you to write the body in interface we had two methods addition subtraction so we got these two men Thirds now this class can have its own method so let's say since it it's advanced calculator I will say calculate sine theta and calculate cos theta any name any data this is just a dummy code you can remove this override so this is mandatory I have to write the body so I will say I am in addition method I am in subtraction method M in advance Kelsea method cos theta method same thing I will write for sine theta method so this class has two method which is its own and two methods are coming from the interfaces and you can see you will get a small arrow here which says these methods are coming from the interface which has been implemented by this particular class now again you will get these four scenarios so this is what we have done we have implemented the calcium face in our Chinese laws so whatever scenarios we have covered in inheritance same scenarios will be applicable here so let us take the first scenario if I create child class reference and child class object this will allow to access all the method of base class and chance laws so in our case advanced calculator is the child's class so if I create object of it let us say obj1 obj2 addition i can call i can call subtraction I can called calculate sine theta and calculate cos theta this is same as we have discussed in the last video so if you are creating object of choice loss which is advanced calculator we are able to call all the method of base class which is nothing but your interface and child class method which is these methods so if I run this code it will directly print all these because we are creating object of child class now let us take one more scenario so I will create copy of it so now we will try to get the second scenario base class reference and child class object this will allow to access all the method of base class only so in our case base class is Kelsey so if I say Kelsey obj 1 equal to new advanced calculator you can see we can only access the base class method which is nothing but your Kelsey interface so you can only call these methods which is coming from the interface so if I run this you will get all the method of base class only now if you give a small attention here you will notice we cannot use third and fourth scenario I will show you directly so if I copy paste the certain code here and third scenario says sorry second scenario which I forward I will implement now second scenario says if I create a base class reference and base class object this will allow to access all the method of base class only so this scenario which is coming from inheritance rate but if you try to do the same thing here Java will not allow you to create object of an interface here what we are trying to do we are creating object of an interface and Java says you cannot create object of an interface the reason is Java says until your class is not fully implemented fully implemented means every method should have a body so interface does not even have a methods body so we cannot create object of an interface so this scenario is invalid and if I take another scenario which says if I create reference of base class in our case its advanced calculator one obj two equal to new Cal see this is again invalid because again we are trying to create object often calculated which is nothing but an interface so Java again says you cannot create object of an interface because it has certain method it does not have even a body and Java says you cannot create object of an interface because it has abstract method what is abstract method abstract methods are those methods it does not have any body at all so you have only two scenarios valid either you can create object of child class which is nothing but advanced calculator in this way you can access all the method of base and child second scenarios you can create reference of base class object of chain class in that case you can access all the method of base class third and four scenarios invalid you cannot create object of the base class here because the base class here we have is the interface and force in air is again invalid so now this comes to a final conclusion why webdriver guys has created a separate interface now just try to assume selenium guys has created one interface called webdriver this webdriver have certain methods let us say get to start a browser close to get the browser get title to print the title of that particular page find the element and so on they have not written the body because the respective class has to write the body now their separate class here called Android driver so even they have separate way to write the code they can write because we are not writing the code we are just giving a blueprint so selenium guys has given this condition this Android driver can have 300 different methods but they should have minimum these methods then only they can access the selenium libraries so they have this condition now let us say I have another class called chrome driver this driver can have 400 methods but they should have minimum these method which has been given by webdriver right so like this they have separate Firefox driver Internet Explorer driver and they have to write the body and the reason why they have not written the body in the interface because every vendor having different implementation so chrome is coming from Google so Google having a different way to start the browser close the browser so they will write the code accordingly again Android is from the Google so they have a separate way to start the Chrome browser in android Mobile's in the same way if you take iPhone driver or I use driver so I use can have 700 methods it does not matter but they should have minimum these methods which has been implemented which has given by the webdriver guys so when they start using this interface you will notice it does not matter which driver you will use you will get all these methods minimum which is really required so that is the reason you you are able to achieve cross browser testing in selenium does not matter which browser you are using but the methods will remain same so if you are not able to understand this webdriver part that's perfectly fine but once you start selenium you have to relate this concept even you are using chrome I Firefox Opera Safari webdriver methods will remain same so that is the reason why they have given webdriver as an interface webdriver has given only the methods and the respect to class has to write the body now before closing this video or in calc Kelsey that is nothing but an interface we have one thing called variable X equal to 10 so let me create a separate class if I say it once calculator three and this time I don't want to write all of this if I say X equal to 90 so you can say you can see here if I try to write X equal to 90 this X is coming actually from the calculator so already we have given ten values if you try to override this value Java will clearly give you one thing called final field cannot be assigned it means you cannot change any value in the class or in other words you cannot change any variable which is coming from the interfaces so if you give x1 then you can give it integer x1 but if you try to write X here X is coming from the calculator or you can say interface so if you try to change the value you cannot change because by default all the variables in interface is final and final value cannot be changed this is another concept now so final variables cannot be changed so this is what I have for this video and hope you understand the concept of interface why they have added and what is the usage of it so the main or gives you the main objective of creating interface just to connect all these classes together so that they can share a common methods name which we can use across our selenium test so now you have a tight relationship between all of these and you can start using it webdriver in a normal way so that is the reason you will always write in this way webdriver driver equal to new Firefox driver it doesn't matter which browser you are using you will get all the methods always same so whenever you say chromedriver methods will remain same whenever you say I driver methods will remain same whenever you say opener driver it does not matter which browser you are using but in the left hand side we have interface rate so this driver will remain same and you will get all the method of web driver which has been written by the selenium guys so that's all I have for this video thank you so much if you still have any doubt let me know thank you so much have a nice day
Info
Channel: Mukesh otwani
Views: 90,683
Rating: 4.9348536 out of 5
Keywords: Selenium, Automation, Webdriver Implementation
Id: 6Er5pIEiscI
Channel Id: undefined
Length: 19min 42sec (1182 seconds)
Published: Sat Jun 18 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.