Inheritance In Java : Tutorials For Selenium WebDriver

Till now we have learnt many tutorials on java software development language like Methods,  Access Modifiers, Static and Non Static, Object, Constructor, and Many More Tutorials On Java... All these java software development object oriented concept tutorials are very much useful in selenium webdriver software automation test case development. That's why I recommend you all to learn them very carefully. All these software automation testing tutorials will also helps you to pass selenium webdriver Interview. Now let me take you one step ahead to Inheritance In java software development language.

What Is Inheritance In Java?
Inheritance(parent-child) Is very useful concept of java object oriented software programming language by which we can reuse the code of parent class. Inheritance Is providing us a mechanism by which we can inherit the properties of parent class In to child class. Example : Audi class Is child class of Car class then Audi class can access/use all the non private properties (methods, variables..etc) of Car class. Using Inheritance, we can reuse the code of parent class In to child class so that size of code will decrease. Maintenance of code will be also very easy.

VIEW EXAMPLE OF USING INHERITANCE IN SELENIUM WEBDRIVER

extends keyword Is used to Inherit child class from parent class. Let me show you very simple example of Inheritance In java. In this example, Car class Is parent class of Audi class so all the non private properties of Car class are Inherited and able to use In Audi class.

Parent Class
public class Car {//Car Class Is Parent Class Of Audi Class
        private String type="Vehicle";
 public static int wheels = 4;
 public String color = "blue";
 String fuel = "Petrol";
 
 public String getfuel(){  
  return fuel;
 }
 
 protected void Seats(){
  int seat = 4;
  System.out.println("Car Seats = "+seat);  
 }
}

Child Class or Sub Class
public class Audi extends Car{//Audi Is child Class Of Car Class.
 public int speed=150;
 
 public static void main(String[] args) {  
  Audi A = new Audi();
  A.printdetails();
  //Can access instance variable of parent class using object reference of child class Inside static methods.
  System.out.println("Color Of Audi = "+A.color);
  //Can access non static method of parent class using object reference of child class Inside static methods.
  System.out.println("Fuel Of Audi = "+A.getfuel());
 }
 
 public void printdetails(){
  //Can access class variables of parent class directly Inside child class non static methods.
  System.out.println("Wheels Of Audi = "+wheels);
  System.out.println("Speed Of Audi = "+speed);
  //Can access non static methods of parent class directly Inside child class non static methods.
  Seats();
                //Can not access private variable of parent class In child class.
  //System.out.println("Wheels Of Audi = "+type);
 }
}

Output of above example will looks like bellow.
Wheels Of Audi = 4
Speed Of Audi = 150
Car Seats = 4
Color Of Audi = blue
Fuel Of Audi = Petrol

If you will study above example carefully, You can understand It easily that In child class, we can access only non private members of parent class. This Is just basic example of Inheritance In  Java.

Overriding
In sub class, When you create a method with same signature, return types and arguments of parent class's method then that sub class's method Is known as overridden method and It Is called as overriding In java. Overriding Is useful to change to the behavior of parent class methods. Let me show you simple example of method overriding In java.

Note : Method overriding is different than method overloading. VIEW POST on method overloading.

Consider same above given example. Generally all cars have 4 seats so we have created Seats() method with 4 seat variable. But supposing ford car has 6 seats and I wants to print 6 seats for Ford car. In this case we can use method overriding In Ford class as bellow. For this example, Consider Car class of above example as parent class of Ford class. Here method Seats() Is overridden In sub class.
public class Ford extends Car{//Ford Is child Class Of Car Class.

 public static void main(String[] args) {
  Car C = new Ford();//Created Ford Class object with Car Class reference.
  C.Seats();
 }
 
 //Parent class method Seats Is overridden In child class.
 protected void Seats(){
  int seat = 6;
  System.out.println("Audi Seats = "+seat);  
 }

}

Output of above example will looks like bellow.
Audi Seats = 6

Here, method of sub class Is called at place of parent class. Another main thing to notice here Is we have created object of child class but reference Is given of parent class. You can view usage of "final" keyword in THIS POST.

8 comments:

  1. In the overriding example, Why the reference is given of the parent class for the child class object? Kindly explain.

    ReplyDelete
  2. I have just added one method to the above given example. Hope this clears your doubt.


    package Test1;

    public class Audi extends Car {

    public void test(){

    int i=10;
    System.out.println("i value is "+i);

    }
    protected void seats()
    {
    int seats=6;
    System.out.println("Total seats available "+seats);

    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Car t=new Audi();
    t.seats(); // this can be accessible by the reference variable as it overrides the parent class method
    t.test(); //this method cannot be accessible as this defined in child class
    }

    }


    As per my knowledge this concept is provide us security (by hiding child class methods)

    ReplyDelete
  3. Hi Prateek,

    To clarify your doubt I have added one more method (test) in Audi class. Please go through the code below:

    package test;

    public class Audi extends Car {

    public void test(){

    int i=10;
    System.out.println("i value is "+i);

    }
    protected void seats()
    {
    int seats=6;
    System.out.println("Total seats available "+seats);

    }



    public static void main(String[] args) {
    // TODO Auto-generated method stub

    car t=new Audi();
    t.seats(); //this can be accessible by the reference variable because seats method was overridden in child class

    t.test(); //this cannot be accessible by the reference variable of parent class as this is a method of child class.

    }

    }

    As per my knowledge this concept provides us security to restrict the access to child class methods and variable.

    ReplyDelete
  4. Inheritance in Java

    The process of obtaining the data members and methods from one class to another class is known as inheritance. Inheritance is one of the important features of object-oriented programming.

    ReplyDelete
  5. Hello all. Great site to learn...thanks
    Car C = new Ford();
    Audi A = new Audi();
    what's the difference between the two objects when one have reference of parent class and the other have reference of child class?

    ReplyDelete
    Replies
    1. @Shay as explained by Himasagar above.. with the first statement Car C = New Ford; Here you have created a object using parent class name and child on the right side. This object will help you in accessing variables of parent class

      The main advantage is suppose the method of parent class is overridden in child class. Then you can access that as well. ONly extra advantage we are getting is overridden method of child is called instead of parent class..

      You would not have got this advantage if you would have written Audi A = new Audi();

      Car C = new Ford();
      This way we get variables of parent class only but overrriden methods of child class

      Delete