public abstract class code example
Example 1: What are abstract methods in java
An abstract method is the method which does’nt have any body.
Abstract method is declared with
keyword abstract and semicolon in place of method body.
public abstract void ();
Ex : public abstract void getDetails();
It is the responsibility of subclass to provide implementation to
abstract method defined in abstract class
Example 2: abstraction in java
Abstraction is defined as hiding internal implementation and showing only
necessary information.
// abstract class
abstract class Addition
{
// abstract methods
public abstract int addTwoNumbers(int number1, int number2);
public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
// non-abstract method
public void printValues()
{
System.out.println("abstract class printValues() method");
}
}
class AbstractMethodExample extends Addition
{
public int addTwoNumbers(int number1, int number2)
{
return number1 + number2;
}
public int addFourNumbers(int number1, int number2, int number3, int number4)
{
return number1 + number2 + number3 + number4;
}
public static void main(String[] args)
{
Addition add = new AbstractMethodExample();
System.out.println(add.addTwoNumbers(6, 6));
System.out.println(add.addFourNumbers(8, 8, 3, 2));
add.printValues();
}
}
Example 3: abstract class in java
public abstract class GraphicObject {
abstract void draw();
}
Example 4: abstract class
Abstract class is used in defining a common super
class while writing Page Object Model layer of the
framework. We usually create an abstract class named
BasePage to have all common members for every page written
in this class example getPageTitle().
Then each Page class (HomePage, LoginPage, DashboardPage
etc.) inherit from BasePage.
Sometimes one may need to change the behavior of methods
implemented in superclass. So, subclass has freedom to
override that method where we use polymorphism.
This is how we use Abstract class in real projects.
.In my framework I have created my
BasePage class as super
class of the all page classes.
I have collected all common elements
and functions into PageBase class and
all other page classes extent PageBase class.
By doing so, I don't have to locate very
common WebElements and it provides
reusability in my framework.
Also
1)Abstract classes cannot be instantiated
2)Abstract class meant to be inherited
so can not be final,static and private
2)An abstarct classes contains abstract method,
concrete methods or both.
3)Any class which extends abstarct class must
override all methods of abstract class
4)An abstarct class can contain either
0 or more abstract method.
Example 5: what are abstract classes in java
Sometimes we may come across a situation
where we cannot provide implementation to
all the methods in a class. We want to leave the
implementation to a class that extends it.
In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBase class as super
class of the all page classes.
I have collected all common elements
and functions into PageBase class and
all other page classes extent PageBase class.
By doing so, I don't have to locate very
common WebElements and it provides
reusability in my framework.
i)List webs=driver.getWindowHandles();
=>create a list first to store web URLs in list
ii)findElements evaluates multiple elements so therefore will assigned to List
iii)To handle dynamic elements store it in the list and identify by index:
List all=driver.findElements(By.tagname(“”)); (or other locators).
Also
1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.
3)Any class which extends abstarct class must
override all methods of abstract class
4)An abstarct class can contain either
0 or more abstract method.