Java – Classes, Objects, and Methods

Classes – To simplify the definition we can say class means it is a category.  For example, mobile handset can be a class (i.e. MobileHandSet) and from which we can create many objects having the same functionalities.   Some more examples of class might be Test Case, Bugs, Log, Exception, Test Book, etc.

Objects – Object is an instance of the class.  We can create multiple instances of an object.

Example:

Public class TestCase {

}

Above is an example of class declared in Java.  Let us create multiple objects using this class:

TestCase tc1 = new TestCase();

TestCase tc2 = new TestCase();

Let us declare one more class:

Public class TestBook {

}

We can create objects of TestBook class as follows:

TestBook tb1 = new TestBook();

TestBook tb2 = new TestBook();

TestBook tb3 = new TestBook();

Methods:

Method is an action that is performed on the object.  For example, a test case object might have a click action.  So inside a class we can declare a method which does the Click action.  As we know, an application might be having multiple UI elements on which Click action needs to be done.

Let us have a look at the following Java program:

package com.pgms.simple;

public class UserActions {
	public void click(String objname){
		System.out.println(objname+" is clicked");
	}

	public void select(String dropdown, String val){
		System.out.println("Value "+val+" is selected from "+dropdown+" Dropdown");
	}
}
package com.pgms.simple;

public class mainclass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserActions ua = new UserActions();
		ua.click("Login button");
		ua.select("Country","India");
	}

}

In the above Java code, I created a class “UserActions” and this class contains two actions “Click” and “Select”.  Click action clicks on various objects including a button, link, image, etc and Select method selects value from a dropdown.  Let us discuss more about Java in the next series of posts on Java.

If the above Java program is executed that produces output as follows:

Login button is clicked
Value India is selected from Country Dropdown

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.