1Abstraction is defined as hiding internal implementation and showing only
2necessary information.
3// abstract class
4abstract class Addition
5{
6 // abstract methods
7 public abstract int addTwoNumbers(int number1, int number2);
8 public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
9 // non-abstract method
10 public void printValues()
11 {
12 System.out.println("abstract class printValues() method");
13 }
14}
15class AbstractMethodExample extends Addition
16{
17 public int addTwoNumbers(int number1, int number2)
18 {
19 return number1 + number2;
20 }
21 public int addFourNumbers(int number1, int number2, int number3, int number4)
22 {
23 return number1 + number2 + number3 + number4;
24 }
25 public static void main(String[] args)
26 {
27 Addition add = new AbstractMethodExample();
28 System.out.println(add.addTwoNumbers(6, 6));
29 System.out.println(add.addFourNumbers(8, 8, 3, 2));
30 add.printValues();
31 }
32}
1bstraction: Hiding the implementation details,
2concentrating on essentials things,
3without worrying about the details
4■ In java, abstraction is achieved via abstract class or interface
5■ Abstraction cannot exist without inheritance
6
73.1. Abstract class a class that's meant to be inherited
8(cannot be private or final)
9■ To create an abstract class: add keyword abstract
10before the class declaration
11■ A class that we cannot create object
12when abstract class extended by regular class:
13we MUST override all the abstract methods of super class
14when abstract class extended by abstract class:
15no need to override any abstract methods
16
17abstract class vs non-abstract class:
18NON-ABSTRACT CLASS:
19cannot have: abstract methods
20can have: constructor, instance method, static method,
21instance block, static block, instance variable, static variable
22ABSTRACT CLASS:
23can have: constructor, instance method, static method, instance block,
24static block, instance variable, static variable...
25ABSTRACT METHOD: method without implementation, meant to be override
26(cannot create object, cannot be final,static,private)
27
28FRAMEWORK EXAMPLE:
29In my framework I have achieved abstraction by using collections
30or Map, because it’s all interface. Most of the cases I come
31across using List. If we want to access elements frequently
32by using index, List is a way to go. ArrayList provides
33faster access if we know index. If we want to store elements and
34want them to maintain an order, List is a better choice.
35i) List webs = driver.getWindowHandles();
36=>create a list first to store web URLs in list
37ii)To handle dynamic elements store it in the list and identify by index
38List all = driver.findElements(By.tagname(“”));
39In my framework I follow POM and had situations where some pages
40shared similar actions that were similar but worked slightly
41different, so I was able to use abstraction to define those
42actions and implement them in each page according to what was needed
43for that webpage
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.
1Abstraction is nothing but the quality of dealing with ideas rather than
2events. It basically deals with hiding the internal details and showing
3the essential things to the user.
1// abstraction in java with example
2// abstract class
3abstract class Addition
4{
5 // abstract methods
6 public abstract int addTwoNumbers(int number1, int number2);
7 public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
8 // non-abstract method
9 public void printValues()
10 {
11 System.out.println("abstract class printValues() method");
12 }
13}
14class AbstractMethodExample extends Addition
15{
16 public int addTwoNumbers(int number1, int number2)
17 {
18 return number1 + number2;
19 }
20 public int addFourNumbers(int number1, int number2, int number3, int number4)
21 {
22 return number1 + number2 + number3 + number4;
23 }
24 public static void main(String[] args)
25 {
26 Addition add = new AbstractMethodExample();
27 System.out.println(add.addTwoNumbers(6, 6));
28 System.out.println(add.addFourNumbers(8, 8, 3, 2));
29 add.printValues();
30 }
31}
1In my framework I have achieved abstraction
2by using collections or Map, because it’s all interface.
3 Most of the cases I come across using List.
4 If we want to access elements frequently by using index,
5List is a way to go. ArrayList provides faster access if we know index.
6 If we want to store elements and want them to maintain an order,
7List is a better choice.
8i) List webs = driver.getWindowHandles();
9=>create a list first to store web URLs in list
10ii) findElements evaluates multiple elements
11so therefore will assigned to List
12iii) To handle dynamic elements store it in the list and identify by index:
13List all = driver.findElements(By.tagname(“”));
14In my framework I follow POM and had situations
15where some pages shared similar actions that were similar
16but worked slightly different, so I was able to use abstraction
17to define those actions and implement them in each page according
18to what was needed for that webpage
19