Selenium WebDriver Java Tutorials - Object In Java

What Is An Object In Java?
If Interviewer ask you this question then your answer should be like this : Object Is an Instance of class. In other words we can say object Is bundle of related methods and variables. Every object has Its own states and behavior. Objects are generally used with constructors In java software development language. Understanding of object Is very Important for Selenium WebDriver software testing tool learner because we have to create and use objects In our test case development of software web applications. We can create multiple objects for the same class having Its own property in java software development language.

Let us take one real world scenario to understand object clearly. Simplest example Is bicycle Is an object of vehicle class having Its own states and behavior. Same way, motorcycle Is another object of vehicle class. Few properties for both the objects are same like wheels=2, handle=1. Few properties for both the objects are different like price, color, speed etc..

How to create object?
You can create object of class vehicle using new keyword as bellow.
public class vehicle { 
 public static void main(String[] args) {
  //Created object for vehicle class using new keyword.
  //bicycle is the reference variable of this object.
  vehicle bicycle = new vehicle("Black");
  
 } 
 //Constructor with color parameter passed. It will retrieve value from object vehicle. 
 public vehicle(String color){
  //Retrieved value will be printed.
  System.out.println("Color Of vehicle Is "+color);  
 } 
}
Console Output will looks like bellow when you will run above example.
Color Of vehicle Is Black

In above example, Used one constructor to pass the value of object. We will look about constructor In my upcoming post. Please remember here one thing - bicycle Is not an object. It Is reference variable of object vehicle. Based on this example, Now we can say that object has three parts as bellow.
  • Declaration : Variable declaration for object. In this example, bicycle is the reference variable for object.
  • Instantiation : Object creation using new keyword Is called as Instantiation.
  • Initialization : Call to a constructor Is known as object initialization.
As described In PREVIOUS POST, You can use object of class to access non static variables or methods of class in different class too. This way you can use object of class and also you can create multiple objects of any class in java software development language.

Bellow given example will show you how to create multiple object of class to pass different kind of multiple values In constructor.
public class vehicle { 
 
 public static void main(String[] args) {
                //Create 2 objects of class. Both have different reference variables.
  vehicle bicycle = new vehicle("black", 2, 4500, 3.7);
  vehicle motorcycle = new vehicle("Blue", 2, 67000, 74.6);

 }
 
 public vehicle(String color, int wheels, int price, double speed){
  System.out.println("Color = "+color+", Wheels = "+wheels+", Price = "+price+", Speed = "+speed);
 } 
}

Output of above program will looks like bellow.
Color = black, Wheels = 2, Price = 4500, Speed = 3.7
Color = Blue, Wheels = 2, Price = 67000, Speed = 74.6

13 comments:

  1. What is the difference b/w Class and Object?Please explain

    ReplyDelete
    Replies
    1. Class is something like a Blueprint of an structure, Where we will be defining the components inside it. Rather, Objects are the real entity created by the structure of the defined class. In class defenition we will be giving what data members should be and what data methods should be. At the time object initialisation, Objects are created by real time memory allocation, till then just a defenition will be available, If we want to use that class we have to create objects for that class and those objects will be used further in your program.

      Delete
  2. object’ refers to an actual instance of a class. Every object must belong to a class.

    ReplyDelete
  3. Vehicle v = new vehicle(). Here Vehicle is a class, and v is a reference variable.

    So class is like a house and v is like a address which points to class

    ReplyDelete
  4. Hi,

    When I try to execute the above code, I get warning highlight under object - bicycle and motorcycle but the code is run successfully and result is diplayed.

    Inthe same code when i remove the objects and try, it is executed without warning and run successfully. Why am I getting that warning?

    Please explain.

    Code that I used is below:

    public class vehicle {

    public static void main(String[] args) {
    new vehicle("black", 2, 4500, 3.7);
    new vehicle("Blue", 2, 67000, 74.6);

    }

    public vehicle(String color, int wheels, int price, double speed){
    System.out.println("Color = "+color+", Wheels = "+wheels+", Price = "+price+", Speed = "+speed);
    }

    }

    thanks!

    ReplyDelete
    Replies
    1. What warning you got? Is it something like the variable never used?

      Vehicle bicycle = new Vehicle("black", 2, 4500, 3.7);
      Vehicle motorcycle = new Vehicle("Blue", 2, 67000, 74.6);
      That's because you haven't used bicycle and motorcycle objects.

      Delete
    2. yes, it says the variable is never used.

      Delete
  5. Vehicle bicycle = new Vehicle("black", 2, 4500, 3.7);
    Vehicle motorcycle = new Vehicle("Blue", 2, 67000, 74.6);

    In the above code , its working fine even if we don't create an object variable like
    Vehicle bicycle , Vehicle motorcycle.
    Only the new object part is required.Then why we are using the object variable if it is not used?
    Please reply

    ReplyDelete
    Replies
    1. Hi Sharaz,
      The above concept involves more into constructors in java.
      public vehicle(String color, int wheels, int price, double speed){
      System.out.println("Color = "+color+", Wheels = "+wheels+", Price = "+price+", Speed = "+speed);
      }

      It is an an constructor which will have same name as class name, and Constructors are called automatically as soon as object gets created using keyword "new", i.e new vehicle(). So that in your above example creating reference i.e "vehicle bicycle" note required.

      Now i will come to your point why we are using the object variable/reference.
      Consider a scenario you will have non static methods like ownerDetails() - this is not a constructor as method name is not same as class name, so we have to call ownerDetails() method explicitly using object reference like bicycle.ownerDetails()

      Summery:
      Reference Variable is used to store the address of a variable or an object, hence we needed reference variable to call member of a class.

      Thanks,

      Delete
  6. Nice explanation shashi.... and thanks to shraz for a question..... lay man like me who is new to programming can understand

    ReplyDelete
  7. Excellent blog with clear details. Thanks much

    ReplyDelete
  8. Nice explanations for beginners.

    ReplyDelete
  9. In above example, You are saying that bicycle is not object. its reference variable. My doubt is where is object then?

    ReplyDelete