Contructor and Encapsulation
Contructor and Encapsulation
1. **Constructor:**
- A **constructor** is a special type of method used to initialize objects
of a class.
- It is called when an object is created and usually sets the initial state
of the object by assigning values to its instance variables.
- **Syntax:** Constructors have the same name as the class and do not
return any type (not even `void`).
- There are two types of constructors:
- **Default constructor**: Takes no arguments.
- **Parameterized constructor**: Takes arguments to initialize an
object with specific values.
- Example:
```java
public class Person {
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
2. **Encapsulation:**
- **Encapsulation** is the concept of bundling the data (fields) and
methods that operate on the data into a single unit, typically a class.
- It also involves restricting direct access to the data by marking the
fields as `private` and providing `public` getter and setter methods to
control access and modification.
- This allows for better control over the values of fields and enforces
rules for data access and modification, promoting **data hiding**.
- Example:
```java
public class Person {
private String name;
private int age;
Key Differences:
- **Constructor** is a mechanism to initialize an object, whereas
**encapsulation** is a design principle to protect data and control how it
is accessed and modified.
- Constructors do not involve data hiding or validation; they are just for
initializing objects. On the other hand, encapsulation ensures the internal
state of an object is protected and only accessible in a controlled manner.
***************************************************************************
***************************************************************************
*********************************
**Example**:
```java
public class WebDriverInitialization {
WebDriver driver;
**Example**:
```java
public class LoginPage {
private WebDriver driver;
private WebElement usernameField;
private WebElement passwordField;
***************************************************************************
***************************************************************************
*********************************
**Example**:
```java
public class Employee {
private String name;
private int age;
**Example**:
```java
public class UserAccount {
private String password;
**Example**:
```java
public class Person {
private String firstName;
private String lastName;
**Example**:
```java
public class Product {
private double price;
### 6. **Immutability**:
- **Reason**: Getters can be used in classes that should not allow
modification of certain fields after initialization, like in immutable classes.
If a field needs to be set once and never changed, you can provide a
getter but omit the setter, ensuring that the field remains constant after
it’s set.
**Example**:
```java
public class Car {
private final String model;
By using getters and setters, you enhance both the robustness and
flexibility of your code.
***************************************************************************
***************************************************************************
*********************************