What are Wrapper Classes in Java?
Introduction
In Java, a wrapper class is a class that wraps around another class or interface, providing a specific interface or set of methods to the wrapped class. This is useful when you want to add additional functionality or behavior to a class without modifying its original implementation. In this article, we will explore the concept of wrapper classes in Java and provide examples of how to use them.
What is a Wrapper Class?
A wrapper class is a class that contains another class or interface as a field or parameter. The wrapper class provides a specific interface or set of methods to the wrapped class, allowing you to access its methods and properties. The wrapper class can also provide additional functionality or behavior to the wrapped class, making it more useful and flexible.
Types of Wrapper Classes
There are two types of wrapper classes in Java:
- Inner Class: An inner class is a class that is defined inside another class. It is a member of that class and can access its members.
- Outer Class: An outer class is a class that contains another class as a field or parameter.
Benefits of Wrapper Classes
Wrapper classes provide several benefits, including:
- Encapsulation: Wrapper classes help to encapsulate the implementation details of the wrapped class, making it harder for other classes to access its internal state.
- Abstraction: Wrapper classes provide a layer of abstraction, allowing you to access the wrapped class’s methods and properties without knowing its internal implementation.
- Flexibility: Wrapper classes can provide additional functionality or behavior to the wrapped class, making it more useful and flexible.
Example of a Wrapper Class
Here is an example of a wrapper class in Java:
public class Address {
private String street;
private String city;
private String state;
private String zip;
public Address(String street, String city, String state, String zip) {
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
In this example, the Address class is a wrapper class that contains the street, city, state, and zip fields. The Address class provides a specific interface to the Address class, allowing you to access its methods and properties.
Example of a Wrapper Class with Inner Class
Here is an example of a wrapper class with an inner class:
public class BankAccount {
private String accountNumber;
private String accountHolderName;
private double balance;
public BankAccount(String accountNumber, String accountHolderName, double balance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountHolderName() {
return accountHolderName;
}
public void setAccountHolderName(String accountHolderName) {
this.accountHolderName = accountHolderName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
In this example, the BankAccount class is a wrapper class that contains the accountNumber, accountHolderName, and balance fields. The BankAccount class provides a specific interface to the BankAccount class, allowing you to access its methods and properties.
Example of a Wrapper Class with Inner Class and Method
Here is an example of a wrapper class with an inner class and a method:
public class Calculator {
private int num1;
private int num2;
public Calculator(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public int add() {
return num1 + num2;
}
public int subtract() {
return num1 - num2;
}
public int multiply() {
return num1 * num2;
}
public int divide() {
if (num2 != 0) {
return num1 / num2;
} else {
throw new ArithmeticException("Cannot divide by zero");
}
}
}
In this example, the Calculator class is a wrapper class that contains the num1 and num2 fields. The Calculator class provides a specific interface to the Calculator class, allowing you to access its methods and properties.
Conclusion
Wrapper classes are a useful tool in Java that can help to encapsulate the implementation details of a class, provide abstraction, and add additional functionality or behavior to a class. By using wrapper classes, you can create more flexible and reusable code that is easier to maintain and extend. In this article, we have explored the concept of wrapper classes in Java and provided examples of how to use them. We have also discussed the benefits of wrapper classes and how to create wrapper classes with inner classes and methods.
