xxxxxxxxxx
Uses of Abstract Classes
Java Abstract class can implement interfaces without even providing the implementation of interface methods. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation. We can run abstract class in java like any other class if it has main() method.
xxxxxxxxxx
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 abstract classes contains abstract method, concrete methods or both.
3)Any class which extends abstract class must override all methods of abstract
class
4)An abstract class can contain either 0 or more abstract method.
xxxxxxxxxx
public abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
public abstract void makeSound(); // Abstract method
}
xxxxxxxxxx
/*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 class
abstract class A{}
// Example of Abstract class that has an abstract method
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
xxxxxxxxxx
Uses of Abstract Classes:-
Java Abstract class can implement interfaces without even providing
the implementation of interface methods. Java Abstract class is used to provide
common method implementation to all the subclasses or to provide default
implementation. We can run abstract class in java like any other class if it
has main() method.
xxxxxxxxxx
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.
xxxxxxxxxx
An abstract class is a common class that has attributes and methods.
and it has at least one abstract method, it can also contain normal methods.
This class cannot be instantiated, it can only be inherited,
that is, it cannot be used to create 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.
xxxxxxxxxx
Abstract classes have some special features:
it's impossible to create an instance of an abstract class;
an abstract class can contain abstract methods that must be implemented in non-abstract subclasses;
it can contain fields and non-abstract methods (including static);
an abstract class can extend another class, including abstract;
it can contain a constructor.
xxxxxxxxxx
<?php
abstract class Cars
{
abstract function car_details();
}
class Maruti extends Cars
{
function car_details()
{
echo "Maruti Details";
}
}
class Honda extends Cars
{
function car_details()
{
echo "<br>Honda Details<br>";
}
}
class Scoda extends Cars
{
function car_details()
{
echo "<br>Scoda Details<br>";
}
}
$obj = new Maruti();
$obj -> car_details();
$obj1 = new Honda();
$obj1 -> car_details();
$obj2 = new Scoda();
$obj2 -> car_details();
?>
xxxxxxxxxx
abstract class Animal { // Base abstract class
public abstract void animalSound(); // This is the abstract method in base class
protected int ageInMonth(int ageInYears){ // shared implementation for derived class
return ageInYears*12;
}
}
class Cat extends Animal {
@Override
public void animalSound() {
System.out.println("Mew"); // Abstract must be implemented in derived class
}
}
abstract class Dog extends Animal { // The derived class is also abstract class
// No need for animalSound() method since this class is an abstract class
}
class Main {
public static void main(String[] args) {
var myAnimal = new Animal(); // Invalid, abstract class cannot be instantiated
var myCat = new Cat(); // valid
var myDog = new Dog(); // Invalid, abstract class cannot be instantiated
myCat.animalSound(); // Forced Polymorphed behavior in derived class 1
}
}