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}
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 // Method abstraction is the practice of reducing inter-dependency between methods.
2 // The following is an unreasonably simple example, but the point stands:
3 // don't make the person on the other side of your function do more work
4 // than they need to. Include all necessary data transformations in your
5 // methods as is. For instance, take method GetAreaOfCircle:
6
7 // Good practice:
8float GetAreaOfCircle(float radius) {
9 return 3.14f * radius * radius;
10}
11int main() {
12 printf("%d", GetAreaOfCircle(3.0f));
13 getch();
14 return 0;
15}
16
17 // Bad practice:
18float GetAreaOfCircle(float radius) {
19 return radius * radius;
20}
21int main() {
22 float area = 3.14f * GetAreaOfCircle(3.0f);
23 printf("%d", );
24 getch();
25 return 0;
26}