Pages

Wednesday, 30 September 2015

Inheritance: Tutorials For Selenium WebDriver

Inheritance : Tutorials For Selenium WebDriver

What Is Inheritance In Java?

Inheritance is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.

Inheritance(parent-child) Is very useful concept of java object oriented 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.

Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.

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

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


No comments:

Post a Comment