Example 1: 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 2: is it necessary for abstract class to have abstract method
No, abstract class can have zero abstract methods.
Example 3: when we should use abstract class
When to use an abstract class
An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.
An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.
If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
If we want to create multiple versions of our component, create an abstract class. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new interface.
Abstract classes have the advantage of allowing better forward compatibility. Once clients use an interface, we cannot change it; if they use an abstract class, we can still add behavior without breaking the existing code.
If we want to provide common, implemented functionality among all implementations of our component, use an abstract class. Abstract classes allow us to partially implement our class, whereas interfaces contain no implementation for any members.
Example 4: what is the need of abstract class
It is helpful if you need to make a generic function that can take a lot of class types as an argument. Eg:
abstract class Shape {
void area();
void perimeter();
}
class Rectangle extends Shape {
int width;
int height;
Rectangle(this.width, this.height);
void area() => this.width * this.height;
void perimeter() => 2*(this.width + this.height);
}
class Triangle extends Shape {
int side1;
int side2;
int side3;
Triangle(this.side1, this.side2, this.side3);
void area() => 0.5 * this.side1 * this.side2 * this.side3;
void perimeter() => this.side1 + this.side2 + this.side3;
}
void printGeometry(Shape shape) {
print("The area of this shape is ${shape.area()}";)
print("The perimeter of this shape is ${shape.perimeter()}";)
}
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.