Inheritance in Java

What is Inheritance?

Defnition 1: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviours of a parent object
Defnition 2: The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance.
Defnition 3: Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.

Why we use Inheritance?

--For Code Re-usability- The biggest advantage of Inheritance is that the code that is already present in base class need not be rewritten in the child class.
--Avoid duplication in code.
--For method overriding

Different terms associated with Inheritance:

Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
There are two ways we can do code reuse either by the implementation of inheritance (IS-A relationship), or object composition (HAS-A relationship)

class Subclass-name extends Superclass-name
          //data members and methods

Types of Inheritance

inheritance in java
Types of Inheritance in Java

Multiple and Hybrid Inheritance
Multiple Inheritance and Hybrid Inheritance in Java

Various Scenarios to create Parent/Child objects:

1. Sub(Child) Class reference and Sub(Child) Object- Allow you to access all the methods and data members of super class and sub class.
2. Super(Parent) Class reference and Sub(Child) Class Object- Allow you to access all the methods and data members of Super class only.
3. Super(Parent) Class reference and Super(Parent) Class Object - Allow you to access all methods and data members of super class only.
4. Sub(Child) Class reference and Super(Parent) Class object- child cannot hold parent object

Usage of inheritance in selenium:

1. We create a Base Class in the Framework to initialize WebDriver interface, WebDriver waits, Property files, Excels, etc., in the Base Class.

2. We extend the Base Class in other classes such as Tests and Utility Class.

To get more details please watch below youtube video and Subscribe the channel.

No comments:

Post a Comment