How to create classes and objects in Java

The focus of this blog is test automation for manual testers.

Test automation is not only about the Selenium WebDriver framework but also about a programming language (Java in our case) and unit testing (JUNIT).

Actually, the most important test automation skill is Java programming

This article is about how classes and objects are created in Java.

The code that follows is about working with triangles.

Any triangle has 3 sides.

For a triangle, one of the measures is the perimeter which is calculated by summing the values of the triangle sides.

The easiest way of calculating a triangle perimeter is

public class mainTriangle {
public static void main(String[] args) {

int x = 3, y = 4, z = 5;

int perimeter = x + y + z;

System.out.println("perimeter value = " + perimeter);

}
}

The code declares 3 numeric variables (using the int data type), one for each triangle side. It assigns a value to each side.

It then declares a numeric variable for the perimeter and it assigns to it the sum of the sides variables.

The last line displays the perimeter value to the console. If you run this code in Eclipse, the result is




Let's assume now that we want to do the same thing for one more triangle. The code changes just a bit:

public class mainTriangle 

public static void main(String[] args)

int x1 = 3, y1 = 4, z1 = 5;

int perimeter1 = x1 + y1 + z1;

System.out.println("perimeter1 value = " + perimeter1);;

int x2 = 6, y2 = 7, z2 = 8;

int perimeter2 = x2 + y2 + z2;

System.out.println("perimeter2 value = " + perimeter2);

}


The result of executing the code is:

perimeter1 value = 12
perimeter2 value = 18




The problems with this code are that

  • it is not clear that x1, y1, z1 (and x2, y2, z2) are the sides of a triangle
  • the code is repeated twice for calculating the perimeter for both triangles

To correct these deficiencies, we will create a class that describes a triangle.

A class is simply a container that defines the data and behaviors of a triangle:

public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
side1 = x;
side2 = y;
side3 = z;
}
}

What are all these things?

Lets take them one by one:

  • Triangle is the name of the class 
  • The class content is included between { }
  • side1, side2, side3 are called class members:
  1. The class members store information about the class. 
  2. In our case, the class members will store the triangle side values. 
  • public Triangle(int x, int y, int z) is called the class constructor:
  1. When an object for the class is created, the class constructor is executed first. 
  2. The class constructor can have parameters (x, y, z). 
  3. It stores the parameter values in the class members so that they can be used at a later time 
  4. The constructor provides the way of supplying information about specific triangle objects and storing it inside of the object.

Each class has objects that are concrete instances of the class.

An object is created as follows:


Triangle triangle1 = new Triangle(3, 4, 5);

Triangle triangle2 = new Triangle(7, 8, 9);



The code for creating the triangle1 object does the following:

  • the class contructor gets the following values for the x, y, z parameters: 3, 4, 5
  • the constructor stores the parameter values in the class members: side1, side2, side3
  • the values for class members for the triangle1 object are 3, 4, 5

So, our code becomes



public class mainTriangle { 


public static void main(String[] args) {

Triangle triangle1 = new Triangle(3, 4, 5);

Triangle triangle2 = new Triangle(7, 8, 9);

}



Executing the code will show no results.

We have added so far definitions of the data that any triangle has (members for the triangle sides).

We need to add now definitions for the behavior of the Triangle class.



Lets add a method to the class.

A method is a way of implementing a behavior that the class objects have.

The method will do something very simple.

It will get the value of one of the sides and return it.

public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
    side1 = x;
    side2 = y;
    side3 = z;
}

public int getSide1()
{
    return side1;
}

}


The method has the following components:

  • getSide1() is the method name
  • the code of the method is included in {}
  • the method returns a numeric value (int)
  • the method will get the value of the side1 class member and provide it (return side1;)
Similar methods can be implemented for the other 2 sides:


public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
    side1 = x;
    side2 = y;
    side3 = z;
}

public int getSide1()
{
    return side1;
}

public int getSide2()
{
    return side2;
}

public int getSide3()
{
    return side3;
}

}


We can add more code to our application:

public class mainTriangle {

public static void main(String[] args) {


Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);


System.out.println("side1 = " + triangle1.getSide1());
System.out.println("side2 = " + triangle1.getSide2());
System.out.println("side3 = " + triangle1.getSide3());

}


The result of executing the code is

side1 = 3
side2 = 4
side3 = 5

We can also calculate the perimeter of triangle1:

public class mainTriangle {

public static void main(String[] args) {


Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);



int perimeter = triangle1.getSide1() + triangle1.getSide2() + triangle1.getSide3();
System.out.println("perimeter = " + perimeter);


}


The result of executing the code is

side1 = 3
side2 = 4
side3 = 5
perimeter = 12




It is very nice and interesting that you can create classes and objects.

But how is this code better than the original one?

It seems that more code is needed when using classes and objects!

For the perimeter calculation, we can create a new method inside of the Triangle class:

public class Triangle{

int side1;
int side2;
int side3;

public Triangle(int x, int y, int z)

{
    side1 = x;
    side2 = y;
    side3 = z;
}

public int getSide1()
{
    return side1;
}

public int getSide2()
{
    return side2;
}

public int getSide3()
{
    return side3;
}

public int getPerimeter()
{
    return side1 + side2 + side3;
}

}

The getPerimeter() method works as follows:

- it gets the values of the side1, side2, side3 members
- it adds them up
- it returns the value of the sum

With the new method, the code gets simpler:

public class mainTriangle {

public static void main(String[] args) {

Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);

System.out.println("perimeter = " + triangle1.getPerimeter());

}


I did not use a variable to store the result of getPerimeter and then display its value.

Instead, I display directly the getPerimeter value.

If the perimeter of the second triangle is needed, just one more line is added:

public class mainTriangle {

public static void main(String[] args) {

Triangle triangle1 = new Triangle(3, 4, 5);
Triangle triangle2 = new Triangle(7, 8, 9);

System.out.println("perimeter1 = " + triangle1.getPerimeter());
System.out.println("perimeter2 = " + triangle2.getPerimeter()); 


}

}


I am hoping that the advantages of creating classes and objects start becoming obvious:

- it is clear in the final code that 3, 4, 5 are side values of a triangle

- the Triangle class defines how the triangle perimeter is calculated; this is done only once and then used by any of the objects of the class; imagine that the we need not one line of code for calculating the perimeter but 5 lines

- there is no code duplication any longer

I hope that this short example showed that programming can be learned and it is not as difficult as you may think.


How about some exercises using your new knowledge? :)

1. Create a rectangle class with methods for calculating the rectangle perimeter and area

2. Add a method to the Triangle class that checks if the triangle is scalene (all sides are different), isosceles (2 sides are equal) or equilateral (all sides are equal)?


If you liked this article and want to provide feedback or want to submit your solutions to the exercises, please use the comments section.

Share this

3 Responses to "How to create classes and objects in Java"

  1. Thank You For your Awosme Tutorials On Selenium Webdriver With Java it will Most Usefull For Begginers like me and more and who are trying to learn own from Web. Once Again Thank you For Your Effort.

    ReplyDelete
    Replies
    1. You are very welcome.

      I am glad that you find the article useful.

      I will have a follow up for it in a few weeks.

      Alex

      Delete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete