Learn Classes And Objects In Java

Hey guys!! Today let’s learn classes and objects in java.

In this post we will see some of the object-oriented aspects of java. First of all let us learn what are classes and how we can declare classes in java and how we can use classes in java. So let’s start

Learn Classes And Objects In Java: what is a class

A class is basically a data structure or type inside which you can define some variables which are called member variables and methods which are called member methods.

Class has got an important property that is you can create instance or object from class. So you can maintain different states of class using instances or objects.

Every java project has at least one class and one main method.

So let’s see how we can define a class,

// class employee

public class Employee {
 
int id;
String name;
int age;

}
public class Sample_Example {

public static void main(String[] args) {
 
Employee john = new Employee(); // john is object or instance
john.id = 05;
john.name = "John Thames";
john.age = 14;
 
System.out.println(john.name + " is " + john.age + " years old.");
 
Employee sam = new Employee(); // sam is object or instance
sam.id = 06;
sam.name = "Sam joseph";
sam.age = 15;
 
System.out.println(sam.name + " is " + sam.age + " years old.");
}
}

Output:

Learn Classes And Objects In Java

We have created Employee class. This class can have member variables and methods. As in the above example we have created employee class. Now we will just trace out the traits of an employee. Employee will have an id of type integer value, employee name of type string and employee age of type integer. This class can also have member methods that we will see in a moment.

Also Read – Best Way To Reverse String Array In Java

Now to call employee class in main method Sample_Example class we have to create an instance of the class. So to do this just call class Employee and give a name to its instance, john. This basically we have created an instance/object of Employee class which we named as john.

Now this object is used to assign values to the member variables of the employee class as in the above example. In similar way we can create different instances of employee class. So using a class we can define different object and we can define object values of object variable which are independent of each other. So as in the above example john instance values are independent of sam instance values. This is very important property of object that it can store its own state and it’s independent of the other instance.

Now in java programming direct usage of variables are discouraged.

john.id = 05;
john.name = "John Thames";
john.age = 14;

Because any other person or whoever is using your code can modify these values. So we use methods which can get or set these values.

What actually these getting and setting values is,

// for example in employee class

public class Employee {
 
int id;
String name;
int age;
 
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

So what this basically doing is whenever we want to assign some value to the ‘id’ we will use ‘set id’ method and whenever we want to get the value of ‘id’ we will use ‘get id’ method and this is called encapsulation of a variable. This is important because we do not want to give access to our variables to some other users which are unknown to us.

Now to assign value to ‘john’ instance we use below format,

public class Sample_Example {

public static void main(String[] args) {
 
Employee john = new Employee(); 
john.setId(05);
john.setName("John Thames");
john.setAge(14);
 
Employee sam = new Employee(); 
sam.setId(06);
sam.setName("Sam joseph");
sam.setAge(15);
 
System.out.println(john.getName() + " is " + john.getAge() + " years old.");
System.out.println(sam.getName() + " is " + sam.getAge() + " years old.");
}
}

Output:

John Thames is 14 years old.
Sam joseph is 15 years old.

When we run the program result will be same but this is more preferred when you are accessing or assigning values to the member variables.

So this is how classes works in java.

About the author

admin

View all posts

Leave a Reply

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