how to initialize abstract class java code example
Example 1: abstract class 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 such case we declare a class
as abstract.To make a class abstract we use key word abstract.
Any class that contains one or more abstract methods is declared as abstract.
If we don’t declare class as abstract which contains abstract methods we get
compile time error.
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.
Example 2: abstract class in java
public abstract class GraphicObject {
abstract void draw();
}
Example 3: abstract class example in java
//abstract parent class
abstract class Animal{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{
public void sound(){
System.out.println("Woof");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.sound();
}
}
Example 4: how to use an abstract class in java
interface methods{
public void hey();
public void bye();
}
//unable to implement all the abstract methods in the interface so
// the other class automatically becomes abstract
abstract class other implements methods{
public void hey(){
System.out.println("Hey");
}
}
//able to implement all the methods so is not abstract
class scratch implements methods {
public void hey(){
System.out.println("Hey");
}
public void bye() {
System.out.println("Hey");
}
}
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.