1it is used to define relationship between two class,
2which a child class occurs all the properties and behaviours of a parent class.
3Provides code reusability.
4Ex: in my framework I have a TestBase class which I store
5all my reusable code and methods. My test execution classes and
6elements classes will extend the TestBase in order to reuse the code.
1public class Bicycle {
2
3 // the Bicycle class has three fields
4 public int cadence;
5 public int gear;
6 public int speed;
7
8 // the Bicycle class has one constructor
9 public Bicycle(int startCadence, int startSpeed, int startGear) {
10 gear = startGear;
11 cadence = startCadence;
12 speed = startSpeed;
13 }
14
15 // the Bicycle class has four methods
16 public void setCadence(int newValue) {
17 cadence = newValue;
18 }
19
20 public void setGear(int newValue) {
21 gear = newValue;
22 }
23
24 public void applyBrake(int decrement) {
25 speed -= decrement;
26 }
27
28 public void speedUp(int increment) {
29 speed += increment;
30 }
31
32}
33
1Builds relations between classes, main purpose:
2create a TEST BASE CLAS and use it in other classes.
3Inheritance allows a class to inherit properties
4(objects, variables, methods) from another source (class or interface).
5Allows code reusability and easy to maintain.
6SUPER CLASS (also known as parent or base class):
7is the class where the fields are being inherited from.
8SUB CLASS (also known as the child or derived class):
9is the class inheriting the properties
10INHERITANCE EXAMPLE
11In my framework I have a TestBase class where I store
12all my reusable code and methods. My test execution classes,
13and elements classes will extend the TestBase in order to reuse the code.
14My framework follow POM and some pages have similar actions,
15so I can easily use those similar actions and fields
16by inheriting them from the ready classes.
17Example: Base Page Class and Test Base Class.
18These 2 class are being inherited from so many different classes.
19For Example; In Pages Package, Base Page Class is being extended
20by all the class by Base Package. So that Constructor
21can be automatically be called in the sub classes.
22That way you will be able to locate the elements
23by using same driver. Test Base Class can also be inheritance.
24One driver, TestNG framework one before method to
25setup browser and reuse it every single test class it.
26By inheriting them to other test class. These 2 class are
27abstract class and meant to be inherited to other classes.
28We are not creating any object in these 2 class.
29These class is super class. Comes from Selenium library WebDriver,
30takes Screenshots, javascriptexecuter these are interface.
31List and Set also interface. You cannot create object in interface.
32They are only being reference. These are also example for abstraction.
33
1#include <iostream>
2
3using namespace std;
4
5class Area
6{
7 public:
8 int getArea(int l, int b)
9 {
10 return l * b;
11 }
12};
13
14class Perimeter
15{
16 public:
17 int getPerimeter(int l, int b)
18 {
19 return 2*(l + b);
20 }
21};
22
23class Rectangle : public Area, public Perimeter
24{
25 int length;
26 int breadth;
27 public:
28 Rectangle()
29 {
30 length = 7;
31 breadth = 4;
32 }
33 int area()
34 {
35 return Area::getArea(length, breadth);
36 }
37 int perimeter()
38 {
39 return Perimeter::getPerimeter(length, breadth);
40 }
41};
42
43int main()
44{
45 Rectangle rt;
46 cout << "Area : " << rt.area() << endl;
47 cout << "Perimeter : " << rt.perimeter() << endl;
48 return 0;
49}
50
1class Vehicle
2{
3 public Vehicle()
4 {
5 System.out.print("Default ");
6 }
7}
8class Car extends Vehicle
9{
10public Car(String carName)
11{
12System.out.print(carName + "");
13}
14}
15public class InheritanceTester
16{
17 public static void main(String[] args)
18 {
19 Car car=new Car("Ford");
20 }
21}
22
1class left {
2public:
3 void foo();
4};
5
6class right {
7public:
8 void foo();
9};
10
11class bottom : public left, public right {
12public:
13 void foo()
14 {
15 //base::foo();// ambiguous
16 left::foo();
17 right::foo();
18
19 // and when foo() is not called for 'this':
20 bottom b;
21 b.left::foo(); // calls b.foo() from 'left'
22 b.right::foo(); // call b.foo() from 'right'
23 }
24};
25