Lesson 8 – Classes and Objects

Classes and objects are the fundamental building blocks of Object Oriented Programming (OOP) paradigm. In OOP, every software entity is implemented as a class. For instance, if you developing an employee management systems, your classes can be a User, Department, Salary, Product etc. In this lesson you will see objects and classes.

A Class

A class in Java is the blue print for the corresponding object. Class defines how an object will look like. A class can have methods and variables. A class physically doesn’t exist in the memory.

Let’s create a simple class “User” with two member variables “name” and “password” and one method “login”.

class User
{
public String name;
public String password;

public void login()
{
System.out.println("Welcome "+ name);
}
}

To create a class, the keyword “class” is used.

An Object

An object actually implements the class. It physically exists on computer’s memory. One class can be used to create multiple objects. To create an object of a class, the keyword “new” is used followed by the class name and opening and closing parenthesis. The following code create object of the User class.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

User u = new User();
u.name = "John";
u.password = "abcd";
u.login();
}
}

In the above code, we create an object “u” of the “User” class. To access a variable or method from an object, you can use the object name followed by the dot operator.  In the above code we have a method “login”. Notice, here we did not specify the method as “static” because the method belongs to the object. The output of the above script is as follows:

Welcome John

Passing Values to Class Method

Just as we can pass values to ordinary methods, we can pass values to class methods as well as explained in the following example:

class User
{
public String name;
public String password;

public void login(String name, String password)
{
this.name = name;
this.password = password;
System.out.println("Welcome "+ name);
}
}

Let’s now create object of the above user class and pass values to the “login” method.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

User u = new User();
u.login("John", "abcd");
}
}

The output will be same:

Welcome John

Class Constructor

A class constructor is basically a simple method which is automatically called when an object of a class is created. The constructor has same name as the class and has no return type. Let’s create a constructor in the User class which can be used to assign values to class members at the time of creation of objects.

class User{

public String name;
public String password;

public User(String name, String password) {
this.name = name;
this.password = password;
}

public void login()
{
System.out.println("Welcome "+ name);
}
}

Here is how you create an object of the class with constructor with parameters:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

User u = new User("John", "abcd");
u.login();
}
}

The output is the same as previous example:

Welcome John

What’s Next

In this lesson, you studied the basics of classes and objects. In the next lesson, you will see inheritance in action.