Classes And Objects

Classes and Objects

 
One of the major advantages of using object-oriented programming is that programming can create modules that are reusable modules that need to be changed when a new type of object is added.
 
While having basic knowledge of the java programming language you can learn to write your own classes. In this article, you can learn about creating or defining your own classes, declaring member variables, methods, and constructors.
 
Class
 
A class is a combination of variables and methods. In real world-class refers to a combined unit having different variables and methods in it.
 
For Example: In daily-life “House” is a class, having Television, Refrigerator, etc as variables and Kitchen. Rooms etc as methods.
 
Declaring a class
 
We can create a class followed by a class keyword followed by class_name. We have seen a class defined in the following way,
  1. class MyClass {  
  2.    // field, constructor, and  
  3.    // method declarations  
  4. }  
This is the class declaration, the class body(between the curly braces}, declarations for the fields that provide the state of class and its objects.
 
Naming Convention for class
 
ClassName should be in camel-case i.e each and every word's first letter must be uppercase.
 
Class declarations can include the following considerations which are,
  • Access Modifiers like public, private, protected, default(no modifier).
  • ClassName must be in camel case order.
  • The class body is surrounded by {}.

Understanding Class Members

 
Class Variables
 
In the case of the Employee class, the instance variables are id, name, salary, email. Each employee object has its own values for these variables, stored in different memory locations.
  1. class Employee {  
  2.     public int emp_id;  
  3.     public String emp_name;  
  4.     public double emp_salary;  
  5.     //add an instance variable for the object ID  
  6.     private int id;  
  7.     //add a class variable for the  
  8.     //number of Bicycle objects instantiated  
  9.     private static int numberOfEmployees = 0;  
  10. }   
Class variables are referenced by the class name itself, as in
  1. Employee.numberOfEmployees  
This makes it clear that they are class variables.
 
Class Methods
 
The Java language provides static methods and variables. Static methods, having static modifiers in their declaration should be invoked by class name, without the need for creating an instance of the class.
 
Syntax - Classname.methodname(params)
 
For Example - Adding a static method to the Employee class to access numberOfEmployees static field.
  1. public static int getNumberOfEmployees() {  
  2.    return numberOfEmployees;  
  3. }  
Declaring Member Variables
 
A Class contains method definitions and variable declaration. There are several types of variables.
  • Variables inside the class - instance variables.
  • Variables inside a block of code or method - local variables.
  • Variables in a class - member variables.
Variables may be instance variables, static variables, or final variables. For example, The Employee class uses the following lines of code.
  1. public int emp_id;  
  2. public String emp_name;  
  3. public double emp_salary;   
The fields of Employee are named emp_id, emp_name, emp_salary. The public access modifier is accessible by any object of the class. Here below is the full description of an Employee class.
  1. class Employee {  
  2.     //Creating properties of Employee class  
  3.     public int emp_id;  
  4.     public double emp_salary;  
  5.     public String emp_name  
  6.     public String emp_email;  
  7.     //Getter and setters for getting and setting properties  
  8.     public int getEmp_id() {  
  9.         return emp_id;  
  10.     }  
  11.     public void setEmp_id(int emp_id) {  
  12.         this.emp_id = emp_id;  
  13.     }  
  14.     public int getSalary() {  
  15.         return emp_salary;  
  16.     }  
  17.     public void setSalary(int emp_salary) {  
  18.         this.emp_salary = emp_salary;  
  19.     }  
  20.     public String getName() {  
  21.         return emp_name;  
  22.     }  
  23.     public void setName(String emp_name) {  
  24.         this.emp_name = emp_name;  
  25.     }  
  26.     public String getEmail() {  
  27.         return emp_email;  
  28.     }  
  29.     public void setEmail(String emp_email) {  
  30.         this.emp_email = emp_email;  
  31.     }  
  32.     //Overriding toString() method  
  33.     @Override  
  34.     public String toString() {  
  35.         return "Employee [emp_id = " + emp_id + ", emp_salary = " + emp_salary + ", emp_name = " + name + ", emp_email = " + emp_email + "]";  
  36.     }  
  37. }  
The fields of Employee are id, name, salary, email is instance variables and setter getter methods are instance methods.
 

Objects & References

 
Once a class is defined, you can declare a variable of type class. A Java program creates many objects, thus a program carries out various tasks. Once an object has completed the work, its resources are recycled for use by other objects.
 
Creating Objects
 
A class provides the blueprint for objects, we can create an object from the class. Here the object of the employee class is created
  1. Employee emp;  
  2. Emp = new Employee();  
  3. //or  
  4. Employee emp = new Employee();  
Instantiating a Class
 
A class is instantiated using a newkeyword as it invokes the object constructor and allocates memory for a newobject and returns the reference of that memory. The newoperator returns a reference to the object is created. This reference is usually assigned to a variable of the appropriate type.
 
The newoperator is used to create an object of that reference type, where emp is the object reference and Employee() is an object.
 
Using Objects
 
Once an object is created we will use that object for some manipulation or retrieve data with that object. We may use this object with one of its fields, or call one of its methods to perform any action.
 
Referencing an object’s field
 
Once an object is created then object fields are accessed by their name. Eg: we can add a statement within the Employee class that prints the Id and name of the employee.
  1. System.out.println("Id and name is: " + id+ ", " + name);  
Code that is outside the class must use an object reference followed by dot(.) operator, followed by simple name,
  1. objectRefrence.fieldName;  
Calling an object’s Method
 
We use an object reference to invoke an object’s method. We append the method’s name with object reference with a dot(.) operator.
 
Syntax - objectReference.MethodName(args); or objectReference.MethodName();
 
The Employee class has some getter methods like getName(), getEmail(), getId(), and more, that represents the name, Email Id, Id of the employee respectively. Here’s employee object invokes the object as,
  1. System.out.println("Name of Employee: " + emp.getName());  
  2. System.out.println("Email Id of Employee: " + emp.getEmail());  
  3. System.out.println("Id of Employee: " + emp.getID());  
These above methods execute and display the results.Remember, invoking a method on a particular object is the same as sending a message to that object.
 

Summary

 
Thus, coming towards the end of this article, we had learned about Classes and Objects and how to use them with Java.
 
What did we learn?
  • What is a Class?
  • Declaring a Class.
  • Understanding Class Members.
  • Declaring Member Variables.
  • Objects: Creation & Using objects
  • Instantiating a class.
  • Referencing an object’s field.
  • Calling an object’s method.