Skip to content

Class & Object

In programming, we have to be able to represent our own data entity. Java allows us to do this by using class and object. We will use java class to represent actual web page under test and utilize them as an object in our test scripts.

In this code note, we will cover following topics:

  • Data Type
  • Defining a Class
  • Creating an Object



› Data Type

In Java, working with data values like text is possible thanks to the String data type. A data that you can use in Java has its own data type and its specifications. These data type specification are expressed using Java class and actually utilized by creating an object.

A simple definition of class and object in java:

  • class:   It is a blueprint for specifying custom data type.

  • object:  Ready to use data that was created based on the class.

To give you an analogy, if you want to use iPhone, first someone has to design and represent what an iPhone is. Based on the design, then it can be manufactured and used as an iPhone object. This creation and usage of object based on its specification is called instantiation in programming.

Similarly in java, if you want to represent your own data type first you have to specify it by creating a class. After the specification and designing is done, you can instantiate an object and start using the data type.

So far all the primitive data we have been working with, we were able to work with them because someone has created data type specifications (Class) for each of these data.

Data Values (Objects) Data Type Representation Document (Class)
-10, 0, 23 integer numbers Integer.java
0.9, 0, -192.23 precision numbers Double.java
'C', '%', '!' characters Character.java
true, false Logics Boolean.java
"Hello", "Good day!" Text String.java

Without these specifications, Java will not be able to recognize these data value (object).



› Defining a Class

Also known as Classification, Java class is a document with .java extension where you describe or specify your custom data type you want Java to recognize.   In this document, you provide following 3 sections for the data type you want to create.

  • sub-data fields
  • operators ( methods )
  • usage mechanism ( constructors )

As an example, representing text data in Java involved specifying following 3 sections:

Once these specifications are finalized, it was then expressed in the Java String class file as follows:

String class
public class String {

 //------- Sub Data Fields ------------- //
    private char[] letters;
    private int uid; 

 //------- Operations ------------------ //
    public int length() {...}

    public boolean equals(String other) {...}

    public void toUpperCase(String original) {...}

    public void toLowerCase(String original) {...}

    public String[] split(String source) {...}

    public int indexOf(String phrase) {...}


  //------- Rule (Usage Mechanism) -------// 
    public String(CharSequence sequence) {
       // code that enables String creations like
       // String var = "some text"; 
    }

    private String(char[] input) {
        // code that enables String creations like 
        // String var = new String("some text");
    }
}//end::class

This capability of Java where it allows programmer to represent anything they want in their program is the primary reason that it is one of the best programming language in the industry.

› Creating an Object

Once the data type specification has been created via Java class. We can use the data type in our Java code as java object. Simply put Java object is working copy of all the code written in the Java class.

Object Creation Syntax:

ClassName variable = new ClassName();

For example for the String.java class that represents textual data, we can use in our Java program by creating an Object from the class as follows:

String name = new String("John Doe");

Let us see another objects created from following Book.java class.

Book class
public class Book {
    //---field
    String title;
    String author;

    //---methods
    public void showInfo(){
        System.out.println("------BOOK INFO------");
        System.out.println("Book Title : " + title);
        System.out.println("Book Author: " + author);
    }
}//end::class

We can use this Book class to represent following two books:

1
2
3
4
5
6
7
Book b1  = new Book();
b1.title ="BDD in Action";
b1.author ="Jan Molak";

Book b2 = new Book();
b1.title ="Paleontology";
b1.author ="David Bainbridge";