Sometimes we may come across a situation where we cannot provide
implementation toall the methods in a class. We want toleave the
implementation toaclass that extendsit. In such case we declare a class
as abstract.To make a classabstract we use key word abstract.
Anyclass 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)Anyclass which extends abstarct class must override all methods of abstractclass4)An abstarct class can contain either 0 or more abstract method.
/*Abstract class:
A class which is declared as abstract is known as an abstract class.
It can have abstract and non-abstract methods. It needs to be extended
and its method implemented. It cannot be instantiated.
*/// Example of abstract classabstractclassA{}// Example of Abstract class that has an abstract methodabstractclassBike{abstractvoidrun();}classHonda4extendsBike{voidrun(){System.out.println("running safely");}publicstaticvoidmain(String args[]){Bike obj =newHonda4();
obj.run();}}
Anabstract method is the method which does’nt have any body.
Abstract method is declared withkeywordabstract and semicolon in place of method body.publicabstractvoid<method name>();Ex:publicabstractvoidgetDetails();It is the responsibility of subclass toprovide implementation toabstract method defined in abstractclass
We cannot create objects of an abstractclass. To implement features of an abstractclass, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstractclass. However,if the subclass is declared abstract, it's not mandatory tooverrideabstract methods.
/**
* Simple Java program to prove that abstract class can have constructor in Java.
* @author http://java67.blogspot.com
*/publicclassAbstractConstructorTest{publicstaticvoidmain(String args[]){Server server =newTomcat("Apache Tomcat");
server.start();}}abstractclassServer{protectedfinalString name;publicServer(String name){this.name = name;}publicabstractbooleanstart();}classTomcatextendsServer{publicTomcat(String name){super(name);}@Overridepublicbooleanstart(){System.out.println(this.name +" started successfully");returntrue;}}Output:ApacheTomcat started successfully
Uses of AbstractClassesJavaAbstractclass can implement interfaces without even providing the implementation of interfacemethods. JavaAbstractclass is used toprovide common method implementation toall the subclasses or toprovidedefaultimplementation. We can run abstractclass in java like any other classif it has main() method.
Sometimes we may come across a situation
where we cannot provide implementation toall the methods in a class. We want toleave the
implementation toaclass that extendsit.
In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBaseclass as superclass of the all page classes.
I have collected all common elements
and functions into PageBaseclass and
all other page classes extent PageBaseclass.
By doing so,I don't have tolocate very
common WebElements and it providesreusability in my framework.
Also1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.3)Anyclass which extends abstarct class must
override all methods of abstractclass4)An abstarct class can contain either
0 or more abstract method.
interface methods{publicvoidhey();publicvoidbye();}//unable to implement all the abstract methods in the interface so // the other class automatically becomes abstractabstractclass other implements methods{publicvoidhey(){System.out.println("Hey");}}//able to implement all the methods so is not abstractclass scratch implements methods {publicvoidhey(){System.out.println("Hey");}publicvoidbye(){System.out.println("Hey");}}
Abstractclass is used in defining a common superclasswhile writing PageObjectModel layer of the
framework. We usually create an abstractclass named
BasePagetohave all common members for every page written
in thisclass example getPageTitle().Then each Pageclass(HomePage,LoginPage,DashboardPage
etc.) inherit from BasePage.
Sometimes one may need tochange the behavior of methods
implemented in superclass. So, subclass has freedom tooverride that method where we use polymorphism.
This is how we use Abstractclass in real projects..In my framework I have created my
BasePageclass as superclass of the all page classes.
I have collected all common elements
and functions into PageBaseclass and
all other page classes extent PageBaseclass.
By doing so,I don't have tolocate very
common WebElements and it providesreusability in my framework.
Also1)Abstract classes cannot be instantiated
2)Abstractclass meant tobe inherited
so can not be final,static and private2)An abstarct classes contains abstract method,
concrete methods or both.3)Anyclass which extends abstarct class must
override all methods of abstractclass4)An abstarct class can contain either
0 or more abstract method.
abstractclassLanguage{// method of abstract classpublicvoiddisplay(){System.out.println("This is Java Programming");}}classMainextendsLanguage{publicstaticvoidmain(String[] args){// create an object of MainMain obj =newMain();// access method of abstract class// using object of Main class
obj.display();}}
Abstract classes have some special features:
it's impossible tocreate an instance of an abstractclass;
an abstractclass can contain abstract methods that must be implemented in non-abstract subclasses;
it can contain fields and non-abstract methods (including static);
an abstractclass can extend another class, including abstract;
it can contain a constructor.
Anabstractclass is a common class that has attributes and methods.
and it has at least one abstract method, it can also contain normal methods.
Thisclass cannot be instantiated, it can only be inherited,
that is, it cannot be used tocreate an object.
Una clase abstracta es una clase común que posee atributos y métodos,
y tiene como mínimo un método abstracto, además puede contener métodos normales.
Esta clase no puede ser instanciada, solo puede heredarse,
o sea no se puede usar para crear un objeto.
Sometimes we may come across a situation
where we cannot provide implementation toall the methods in a class. We want toleave the
implementation toaclass that extendsit.
In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBaseclass as superclass of the all page classes.
I have collected all common elements
and functions into PageBaseclass and
all other page classes extent PageBaseclass.
By doing so,I don't have tolocate very
common WebElements and it providesreusability in my framework.
i)List<String> webs=driver.getWindowHandles();=>create a list first tostore web URLs in list
ii)findElements evaluates multiple elements so therefore will assigned toList<WebElement>
iii)To handle dynamic elements store it in the list and identify by index:List<WebElement> all=driver.findElements(By.tagname(“”));(or other locators).Also1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.3)Anyclass which extends abstarct class must
override all methods of abstractclass4)An abstarct class can contain either
0 or more abstract method.
// create an abstract classabstractclassLanguage{// fields and methods}...// try to create an object Language// throws an errorLanguage obj =newLanguage();