what is inheritance code example
Example 1: inheritance in java
class Animal
{
void eat()
{
System.out.println("Animal is eating.");
}
}
class Lion extends Animal
{
public static void main(String[] args)
{
Lion obj = new Lion();
obj.eat();
}
}
Example 2: what is inheritance
it is used to define relationship between two class,
which a child class occurs all the properties and behaviours of a parent class.
Provides code reusability.
Ex: in my framework I have a TestBase class which I store
all my reusable code and methods. My test execution classes and
elements classes will extend the TestBase in order to reuse the code.
Example 3: Inheritance
Builds relations between classes, main purpose:
create a TEST BASE CLAS and use it in other classes.
Inheritance allows a class to inherit properties
(objects, variables, methods) from another source (class or interface).
Allows code reusability and easy to maintain.
SUPER CLASS (also known as parent or base class):
is the class where the fields are being inherited from.
SUB CLASS (also known as the child or derived class):
is the class inheriting the properties
INHERITANCE EXAMPLE
In my framework I have a TestBase class where I store
all my reusable code and methods. My test execution classes,
and elements classes will extend the TestBase in order to reuse the code.
My framework follow POM and some pages have similar actions,
so I can easily use those similar actions and fields
by inheriting them from the ready classes.
Example: Base Page Class and Test Base Class.
These 2 class are being inherited from so many different classes.
For Example; In Pages Package, Base Page Class is being extended
by all the class by Base Package. So that Constructor
can be automatically be called in the sub classes.
That way you will be able to locate the elements
by using same driver. Test Base Class can also be inheritance.
One driver, TestNG framework one before method to
setup browser and reuse it every single test class it.
By inheriting them to other test class. These 2 class are
abstract class and meant to be inherited to other classes.
We are not creating any object in these 2 class.
These class is super class. Comes from Selenium library WebDriver,
takes Screenshots, javascriptexecuter these are interface.
List and Set also interface. You cannot create object in interface.
They are only being reference. These are also example for abstraction.
Example 4: inheritance
class Employee(): def __init__(self, emp_id, salary): self.emp_id = emp_id self.salary = salary def give_raise(self): self.salary = self.salary * 1.05
Example 5: inheritance in oops
it is used to define relationship between two class,
which a child class occurs all the properties and
behaviours of a parent class.
Provides code reusability. We can implement by using
extend keyword
Ex: in my framework I have a TestBase class which I store
all my reusable code and methods. My test execution classes and
elements classes will extend the TestBase in order to reuse the code.