Answers for "when should i use abstract classes"

2

is it necessary for abstract class to have abstract method

Abstract classes CAN have non-abstract methods.
Posted by: Guest on August-27-2021
2

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.
Posted by: Guest on May-28-2021
1

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()}";)
}
Posted by: Guest on April-19-2021

Code answers related to "when should i use abstract classes"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language