AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Inheritance in oops3/14/2023 ![]() ![]() We can see that the name is always displayed in the corner, and the menu (About, Support, Login) is always available: After logging in, we can see that many elements are common, whether the logged-in user is an Admin or not, and regardless of the landing page. I will return to the same example I used in the previous post, which is the OrangeHRM demo site. Let’s see how this looks with an actual example: Inheritance example in test automation Base page In this case, it’s useful to have a base test class, which all the other test classes will inherit. For example, when the application we test has some common elements on all pages: headers, side menus, etc.Īnother scenario is where we have the same setup and teardown for all our tests. In test automation, inheritance can be very useful when we use the page object model, and we want to have a base page. Protected void Drink() => Console.WriteLine("Drinking") Public void Eat() => Console.WriteLine("Eating") In addition, if the inherited class has any protected methods, this can be accessed from within the derived classes, without instantiating an object of the parent class: class Animal When we create new instances of the Dog or the Cat classes, we will be able to use the methods inside these classes, as well as the ones from the class they inherit (Animal): Dog dog = new Dog() Public void Meow() => Console.WriteLine("Meowing") Public void Bark() => Console.WriteLine("Barking") This is how the two classes can look: class Dog : Animal The syntax for inheritance in C# is a semicolon between the class names. Next, we can have two separate classes, Cat and Dog, which are derived classes of the Animal class. Public void Sleep() => Console.WriteLine("Sleeping") In this class, we have methods that apply to any kind of animal, for example, Eat() and Sleep(): public void Eat() => Console.WriteLine("Eating") Let’s assume we have a parent class, called Animal. ![]() The easiest way to understand inheritance is through examples. This makes the code easy to be reused, as well as easily maintained ✅ If any changes need to be done, you can do them directly in the parent class. This means that the protected and public members of the parent class can be reused in the derived class, without having to define them all over again. Inheritance in OOP is achieved when one object acquires (inherits) the properties and the behaviors of the parent object. What is inheritance in OOP? Inheritance definition After understanding encapsulation, let’s understand inheritance and why it has a very important role in code reusability □ Table of Contents This is the second part of my OOP principles series in test automation with inheritance. ![]()
0 Comments
Read More
Leave a Reply. |