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
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}
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.
16Also
171)Abstract classes cannot be instantiated
182)An abstarct classes contains abstract method,
19concrete methods or both.
203)Any class which extends abstarct class must
21 override all methods of abstract class
224)An abstarct class can contain either
23 0 or more abstract method.
1// example on abstract class in java
2import java.util.*;
3// abstract class
4abstract class Shape
5{
6 // abstract method
7 abstract void sides();
8}
9class Triangle extends Shape
10{
11 void sides()
12 {
13 System.out.println("Triangle shape has three sides.");
14 }
15}
16class Pentagon extends Shape
17{
18 void sides()
19 {
20 System.out.println("Pentagon shape has five sides.");
21 }
22 public static void main(String[] args)
23 {
24 Triangle obj1 = new Triangle();
25 obj1.sides();
26 Pentagon obj2 = new Pentagon();
27 obj2.sides();
28 }
29}