Selenium Webdriver Day 1 : Class and object

To understand both topics first we have to learn about OOPS(Object Oriented Programming) Technique.
In Object oriented programming we have to design a program using Objects and
Classes. OOPS provide you the freedom from writing down the complete code within same class , it allow you to write down the code anywhere and access that code using objects. OOPS have some basic feature like (Abstraction, Encapsulation, Polymorphism and Inheritance ), we will discuss about this on later chapter.
Class : In java class is like a paperwork in which we define details of an object. Also we can say that this is a logical entity.
Example : Human is a Class.
Object : An Object is a real time entity . We can also say that an object is a logical as well as physical entity. In java objects are created to use methods and variables defined inside the class.In java JVM(java virtual machine)  is responsible to create an object.
Example : An individual human in this earth can be defined as an object which have their own properties.
Let us go to Real life example to understand the concept of Object and Classes . Let us take JVM(java virtual machine) as a God. So suppose God created Human class in which he defined the basic properties of human being(Like walking, talking,eating etc) so he can easily create new human whenever he want.But to create new human being every time God need to create new object of his human class and with the help of new created object he can call methods (Properties)  of human.
Programming explanation  : Look at the Human class below, in which two methods are defined (talking and walking) both methods are properties of human class:
public class Human {
//Method for walking
public void talking(){
System.out.println("Human can talk");
}
//method for talking
public void walking(){
System.out.println("Human can walk");
}
}
Now after defined the Class and the properties of that class we can not us them until and unless we will create an object of this class. To create an object "new" keyword is defined in java.
Human h=new Human();
Above code is used to create object of Human class.
new Human():
As already discussed "new " keyword is used to create now object of class in memory and Human() is default constructor of Human class, which will be created automatically by JVM. Basically work of default constructor us to store all the variables and methods into the object and return a reference so we can use them.
Now have a look on
Human h
Here "h" is reference variable of Human type. This is created Human type because only Human can understand that.
Now to use the methods defined inside the Human class we need to write down the below line :
h.talking(); / /  to call the talking method
Now the complete code will look like this :
public class Human {
//Method for walking
public void talking(){
System.out.println("Human can talk");
}
//method for talking
public void walking(){
System.out.println("Human can walk");
}
public static void main(String arg[]) {
Human h=new Human();
h.talking();
}
}
Note : 
  1. JVM will create no argument constructor automatically by default.
  2. Default constructor load all the variables and methods into the objects.

Popular posts from this blog

Selenium Webdriver Day 4 : Inheritance

Python Programming - Log file is not capturing the data?