Friday, 25 August 2017

Java - Object creation and Data encapsulation

Object creation

A java class is the definition of a data structure plus actions that operate on them which typically corresponds to a real world object or concept. Constructors are used to initialize the member variables of a class. When an object is created, memory is allocated for the object and the constructor for the class is executed. Constructors are used to initialize an object. Whenever an object is created, a constructor executes. A default constructor is the one that has no arguments and is provided automatically for all classes. This constructor will initialize all instance variables to default values.
Example: the following class use two constructors, the first (line 8) has no parameters while the second (line 13) one has one so you can set the value of name during the object creation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.blogspot.informationtechnologyarchive;

public class Employee {

 private String name;
 
 // Constructor 1 without parameters
 public Employee() {
  this.name = "Jack";
 }
 
 // Constructor 2 with a parameter
 public Employee(String name){
  this.name = name;
 }
 
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

}
Objects are created using the new keyword. The keyword is used in conjunction with a classname and results in memory being allocated from the heap for the object.
Continuing the previous example, we creating two employee objects with both constructors:
1
2
3
4
5
6
 public static void main(String[] args) {
  Employee employee1 = new Employee();
  Employee employee2 = new Employee("Bob");
  System.out.println("Employee 1: " + employee1.getName());
  System.out.println("Employee 2: " + employee2.getName());
 }
The output will be:
Employee 1: Jack
Employee 2: Bob

Data encapsulation

Data encapsulation is concerned with hiding irrelevant information from the programmer and exposing the relevant information. Hiding the implementation details allow changes without affecting other parts of the program.
As example, consider the implementation of the Employee class, the instance variable was declared as private (line 5) and the methods getName and setName were declared as public so you can read and write the name fied but if the metod setName was declared as private then you can only read the name.

Default constructors

A default constructor is normally present for a class. If a class does not have any constructors explicitly declared, it automatically has a default constructor. A default constructor is a constructor that has no arguments.
As example we change Employee class using a default constructor:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.blogspot.informationtechnologyarchive;

public class Employee {

 private String name;
 
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

}

Referencing instance variables

A reference variable holds a reference, or pointer, to an object. A field or variable of the object is accessed by following the object reference variable name with a period and then the field or method name. The following code snippet illustrates possible references using the Employee class based upon the declaration of Employee found in the previous section:

1
2
Employee employee = new Employee();
String name = employee.getName();

No comments:

Post a Comment

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...