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.
1An abstract method is the method which does’nt have any body.
2Abstract method is declared with
3keyword abstract and semicolon in place of method body.
4
5 public abstract void <method name>();
6Ex : public abstract void getDetails();
7It is the responsibility of subclass to provide implementation to
8abstract method defined in abstract class
1
2interface human {
3 void dothings();
4 void eatthings();
5 void sleep();
6}
7abstract class student implements human {
8 public void dothings()
9 {
10 System.out.println("doing something");
11 }
12 public void eatthings()
13 {
14 System.out.println("eating something");
15 }
16}
17class basisstudent extends student {
18 public void sleep()
19 {
20 System.out.println("no");
21 }
22}
23public class Main {
24 public static void main(String[] args)
25 {
26 basisstudent you = new basisstudent();
27 you.dothings();
28 you.eatthings();
29 you.sleep();
30 }
31}
32
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
1Abstract class is used in defining a common super
2class while writing Page Object Model layer of the
3framework. We usually create an abstract class named
4BasePage to have all common members for every page written
5in this class example getPageTitle().
6Then each Page class (HomePage, LoginPage, DashboardPage
7etc.) inherit from BasePage.
8Sometimes one may need to change the behavior of methods
9implemented in superclass. So, subclass has freedom to
10override that method where we use polymorphism.
11This is how we use Abstract class in real projects.
12
13.In my framework I have created my
14BasePage class as super
15class of the all page classes.
16I have collected all common elements
17and functions into PageBase class and
18all other page classes extent PageBase class.
19By doing so, I don't have to locate very
20common WebElements and it provides
21reusability in my framework.
22Also
231)Abstract classes cannot be instantiated
242)Abstract class meant to be inherited
25 so can not be final,static and private
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.
1// abstract class
2abstract class Shape
3{
4 // abstract method
5 abstract void sides();
6}
7class Triangle extends Shape
8{
9 void sides()
10 {
11 System.out.println("Triangle shape has three sides.");
12 }
13}
14class Pentagon extends Shape
15{
16 void sides()
17 {
18 System.out.println("Pentagon shape has five sides.");
19 }
20 public static void main(String[] args)
21 {
22 Triangle obj1 = new Triangle();
23 obj1.sides();
24 Pentagon obj2 = new Pentagon();
25 obj2.sides();
26 }
27}