1Sometimes we may come across a situation where we cannot provide
2implementation to all the methods in a class. We want to leave the
3implementation to a class that extends it. In such case we declare a class
4as abstract.To make a class abstract we use key word abstract.
5Any class that contains one or more abstract methods is declared as abstract.
6If we don’t declare class as abstract which contains abstract methods we get
7compile time error.
8
9 1)Abstract classes cannot be instantiated
10 2)An abstarct classes contains abstract method, concrete methods or both.
11 3)Any class which extends abstarct class must override all methods of abstract
12 class
13 4)An abstarct class can contain either 0 or more abstract method.
1public abstract class Account { //abstract class //perent class
2 protected int accountNumber;
3 protected Customer customerObj;
4 protected double balance;
5 //constructor
6 public Account(int saccountNumber, Customer scustomerObj,double sbalance){
7 accountNumber = saccountNumber;
8 customerObj = scustomerObj;
9 balance = sbalance;
10 }
11 // abstract Function
12 public abstract boolean withdraw(double amount);
13}
14
15public class SavingsAccount extends Account { // child class
16 private double minimumBalance;
17 // constructor
18 public SavingsAccount(int saccountNumber, Customer scustomerObj, double sbalance, double sminimumBalance) {
19 super(saccountNumber, scustomerObj, sbalance);
20 minimumBalance = sminimumBalance;
21 }
22 // Implementation of abstract function in child class
23 public boolean withdraw(double amount) {
24 if (balance() > minimumBalance && balance() - amount > minimumBalance) {
25 super.setBalance(balance() - amount);
26 return true;
27 } else {
28 return false;
29 }
30 }
31}
32
33
1interface methods{
2 public void hey();
3 public void bye();
4}
5
6//unable to implement all the abstract methods in the interface so
7// the other class automatically becomes abstract
8abstract class other implements methods{
9 public void hey(){
10 System.out.println("Hey");
11 }
12}
13
14//able to implement all the methods so is not abstract
15class scratch implements methods {
16 public void hey(){
17 System.out.println("Hey");
18 }
19 public void bye() {
20 System.out.println("Hey");
21 }
22}
1//abstract parent class
2abstract class Animal{
3 //abstract method
4 public abstract void sound();
5}
6//Dog class extends Animal class
7public class Dog extends Animal{
8
9 public void sound(){
10 System.out.println("Woof");
11 }
12 public static void main(String args[]){
13 Animal obj = new Dog();
14 obj.sound();
15 }
16}
1Sometimes we may come across a situation
2where we cannot provide implementation to
3all the methods in a class. We want to leave the
4implementation to a class that extends it.
5 In that case we declare a class
6as abstract by using abstract keyword on method
7signature.In my framework I have created my
8PageBase class as super
9class of the all page classes.
10I have collected all common elements
11and functions into PageBase class and
12all other page classes extent PageBase class.
13By doing so, I don't have to locate very
14common WebElements and it provides
15reusability in my framework.
16
17i)List<String> webs=driver.getWindowHandles();
18=>create a list first to store web URLs in list
19
20ii)findElements evaluates multiple elements so therefore will assigned to List <WebElement>
21
22iii)To handle dynamic elements store it in the list and identify by index:
23List<WebElement> all=driver.findElements(By.tagname(“”)); (or other locators).
24Also
251)Abstract classes cannot be instantiated
262)An abstarct classes contains abstract method,
27concrete methods or both.
283)Any class which extends abstarct class must
29 override all methods of abstract class
304)An abstarct class can contain either
31 0 or more abstract method.