Java OOPS:Encapsulation | Selenium
Encapsulation in Java/selenium
Encapsulation is the process of binding/encapsulating the data and code together. It is used to hide the implementation details. The encapsulation can be achieved by the use of 3 keywords in java.
private : The variables or methods that have been declared as private will not be accessible outside the class, But the same class member can access private methods and variables in the class.
protected : The variable or methods that have been declared as protected will be accessible inside the class in which they have been defined and in the classes which extend this class. Protected Getter and Protected setter can be used to give access to only their family members.
public : The variable or methods that have been declared as public will be accessible anywhere. Also, a class can be made public.
Mostly we use private, and public methods for encapsulation, all the important details which we do not want to disclose to other members will be part of the private method.
But somehow we want to let the user know the result of the private method, so we will create a public method in a way that the method will show only the result of the private method to the user but not the implementation of the private method.
In case if we want to give more access to the same family member, the inherited class then we create some protected methods which will provide more details of the private method result which outside members of the family cannot see.
In my Language : Consider an organization as a private method, employees as inherited class methods, General public or press as Outside members.
For every Quarter, the organization releases a newsletter stating the Profits/Losses to the press and employees, but the same organization gives details about which project made what profit to the employees through the internal newsletter.
In the below example we allow the user to set and get the name of the employee using getter and setter, but we do not reveal any other details like how the name is processed and what is the operation happening to make up the emp name
package newtest;
class EncapsulationExample{
private String empName;
private void modifyEmpName(String newName) {
empName = newName + " : He acts in BigBang Theory";
}
public String getEmpName(){
return empName;
}
public void setEmpName(String newName){
modifyEmpName(newName);
}
}
public class EncapsulationTest{
public static void main(String args[]){
EncapsulationExample obj = new EncapsulationExample();
obj.setEmpName("Sheldon Cooper");
System.out.println(obj.getEmpName());
}
}
Wrapper classes in Java
Wrapper classes in Java
As their name suggests, they are used to wrap the primitive data types into an Object type. The primitive data types are not objects, so they do not belong to any class.
Collections only store and support objects, so it is required to convert the primitive type to object first, which we can do by using wrapper classes.

All the wrapper classes are subclasses to Object Class, All the number-related classes are sub-class to Number class. The Number class is an abstract class in Java.
Note: There are four other subclasses of Numbers that are not discussed here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications.
Wrapper classes in Java
As their name suggests, they are used to wrap the primitive data types into an Object type. The primitive data types are not objects, so they do not belong to any class.
Collections only store and support objects, so it is required to convert the primitive type to object first, which we can do by using wrapper classes.
All the wrapper classes are subclasses to Object Class, All the number-related classes are sub-class to Number class. The Number class is an abstract class in Java.
Note: There are four other subclasses of Numbers that are not discussed here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications.
Boolean class (for boolean) :
Boolean wrapper class converts the boolean primitive data into Boolean Object.
Boolean wrapper class converts the boolean primitive data into Boolean Object.
Number class (for number) :
All the number related classes are inherited from the Number class.
All the number related classes are inherited from the Number class.
Character class (for character) :
Character wrapper class converts the character primitive values into a character object.
Character wrapper class converts the character primitive values into a character object.
Byte class (for byte) :
Byte wrapper class converts the byte value into Byte Object
Byte wrapper class converts the byte value into Byte Object
Integer class (for int) :
Integer class converts the primitive value into Integer values; remaining wrapper classes also converts primitive values into respective Object value.
We will be discussing the methods present in the Integer class. Almost all the remaining classes have similar methods only.
Integer class converts the primitive value into Integer values; remaining wrapper classes also converts primitive values into respective Object value.
We will be discussing the methods present in the Integer class. Almost all the remaining classes have similar methods only.
Integer Wrapper class
The Integer class has two constructors: 1. Integer(int i) : accepts the integer parameter and creates the object for the int.
Integer intObj1 = new Integer(10);
2. Integer(String s) : accepts String parameter, converts the accepted string parameter into int and creates Integer object for the int.
Integer intObj2 = new Integer("89");
Instead of using the above two forms, it is always better to use the below way of creating the object and which takes less memory.
Integer i = Integer.valueOf("100");
Below are few non-static methods present in the Integer class
The Integer class has two constructors: 1. Integer(int i) : accepts the integer parameter and creates the object for the int.
Integer intObj1 = new Integer(10);
2. Integer(String s) : accepts String parameter, converts the accepted string parameter into int and creates Integer object for the int.
Integer intObj2 = new Integer("89");
Instead of using the above two forms, it is always better to use the below way of creating the object and which takes less memory.
Integer i = Integer.valueOf("100");
Below are few non-static methods present in the Integer class
byteValue() :
Returns the value of this Integer as a byte.
Returns the value of this Integer as a byte.
compareTo(Integer anotherInteger) :
Compares two Integer objects numerically.
Compares two Integer objects numerically.
intValue() :
Returns the value of this Integer as an int.
Returns the value of this Integer as an int.
toString() :
Returns a String object representing this Integer's value. Below are few static methods present in the Integer class.
Returns a String object representing this Integer's value. Below are few static methods present in the Integer class.
parseInt(String s) :
Parses the string argument as a signed decimal integer.
Parses the string argument as a signed decimal integer.
valueOf(String s) :
Returns an Integer object holding the value of the specified String.
Returns an Integer object holding the value of the specified String.
toString(int i) :
Returns a String object representing the specified integer.
Reasons to use Number class than primitive values :
There are three reasons that you might use a Number object rather than a primitive
- As an argument of a method that expects an object (often used when manipulating collections of numbers).
- To use constants defined by the class, such as
MIN_VALUE and MAX_VALUE
, that provide the upper and lower bounds of the data type. - To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Features of the Java wrapper Classes
- Wrapper classes convert numeric strings into numeric values.
- The way to store primitive data in an object.
- The valueOf() method is available in all wrapper classes except Character
- All wrapper classes have the
typeValue()
method. This method returns the value of the object as its primitive type.
Reasons for using Wrapper classes :
- Allows null values
- It can be used in collections such as List, Map, etc.
- It can be used in methods that accept arguments of the Object type.
- Makes available all the functions that Object class has such as clone(), equals(), hashCode(), toString() etc.
Returns a String object representing the specified integer.
Reasons to use Number class than primitive values :
There are three reasons that you might use a Number object rather than a primitive
- As an argument of a method that expects an object (often used when manipulating collections of numbers).
- To use constants defined by the class, such as
MIN_VALUE and MAX_VALUE
, that provide the upper and lower bounds of the data type. - To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Features of the Java wrapper Classes
- Wrapper classes convert numeric strings into numeric values.
- The way to store primitive data in an object.
- The valueOf() method is available in all wrapper classes except Character
- All wrapper classes have the
typeValue()
method. This method returns the value of the object as its primitive type.
Reasons for using Wrapper classes :
- Allows null values
- It can be used in collections such as List, Map, etc.
- It can be used in methods that accept arguments of the Object type.
- Makes available all the functions that Object class has such as clone(), equals(), hashCode(), toString() etc.
Boxing :
The process of converting the primitive data type into an Object using the Wrapper class is called Boxing. For example, converting int to Integer.
The process of converting the primitive data type into an Object using the Wrapper class is called Boxing. For example, converting int to Integer.
Auto-Boxing :
Auto-boxing is nothing but converting primitive data into Objects automatically; this is the reason we are able to store our primitive values in the Collections.
The Java compiler can directly convert the primitive types into corresponding objects. This process is known as auto-boxing. Auto-boxing happens-
- when a method is expecting a wrapper class object but the value that is passed as a parameter is a primitive type. Example-
public static void autoboxMethod(Integer num){
System.out.println(num);
}
public static void main(String[] args) {
//passed int (primitive type) would be converted to Integer object at Runtime
//autoboxing
autoboxMethod(14);
}
Output-
14
- When at some point of time, you are assigning a primitive type value to an object of its wrapper class. The compiler does the autoboxing at runtime.
public static void main(String args[]){
int a=14;
//autoboxing
Integer obja=new Integer(a);
//autoboxing
Integer b=5;
System.out.println(obja);
System.out.println(b);
}
Output-
14
5
- When dealing with collection framework classes. In the below example, here ArrayList class is expecting an Integer wrapper class object but we are providing int primitive. So autoboxing happens.
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
//autoboxing
list.add(1);
list.add(2);
System.out.println("ArrayList: " + list);
}
Output-
ArrayList: [1, 2]
Auto-boxing is nothing but converting primitive data into Objects automatically; this is the reason we are able to store our primitive values in the Collections.
The Java compiler can directly convert the primitive types into corresponding objects. This process is known as auto-boxing. Auto-boxing happens-
- when a method is expecting a wrapper class object but the value that is passed as a parameter is a primitive type. Example-
public static void autoboxMethod(Integer num){
System.out.println(num);
}
public static void main(String[] args) {
//passed int (primitive type) would be converted to Integer object at Runtime
//autoboxing
autoboxMethod(14);
}
Output-
14
- When at some point of time, you are assigning a primitive type value to an object of its wrapper class. The compiler does the autoboxing at runtime.
public static void main(String args[]){
int a=14;
//autoboxing
Integer obja=new Integer(a);
//autoboxing
Integer b=5;
System.out.println(obja);
System.out.println(b);
}
Output-
14
5
- When dealing with collection framework classes. In the below example, here ArrayList class is expecting an Integer wrapper class object but we are providing int primitive. So autoboxing happens.
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
//autoboxing
list.add(1);
list.add(2);
System.out.println("ArrayList: " + list);
}
Output-
ArrayList: [1, 2]
Un-Boxing :
Unboxing is the process of converting the primitive values which are wrapped as Objects into primitive values.
Unboxing happens when-
- The method is expecting Integer object (parameter) but we have given int. In such a case automatic conversion(unboxing) happens that converts Integer to int.
public static void unboxMethod(int num){
System.out.println(num);
}
public static void main(String[] args) {
Integer num = new Integer(14);
//passed Integer wrapper class object
//would be converted to int primitive type at Runtime
unboxMethod(num);
}
Output-
14
- During assignments like-
public static void main(String args[]){
Integer i=new Integer(14);
//unboxing int type to Integer object
int a=i;
System.out.println(a);
}
Output-
14
- When dealing with collection framework classes-
In the below example, the object is automatically converted into the primitive type int and assigned to the variable a-
public static void main(String[] args) {
ArrayList < Integer > list = new ArrayList < > ();
//autoboxing
list.add(1);
list.add(2);
System.out.println("ArrayList is " + list);
// unboxing
//here the get() method returns the object at the index 0
int a = list.get(0);
System.out.println("Value at index 0 is " + a);
}
Output-
ArrayList is [1, 2]
Value at index 0 is 1
Unboxing is the process of converting the primitive values which are wrapped as Objects into primitive values.
Unboxing happens when-
- The method is expecting Integer object (parameter) but we have given int. In such a case automatic conversion(unboxing) happens that converts Integer to int.
public static void unboxMethod(int num){
System.out.println(num);
}
public static void main(String[] args) {
Integer num = new Integer(14);
//passed Integer wrapper class object
//would be converted to int primitive type at Runtime
unboxMethod(num);
}
Output-
14
- During assignments like-
public static void main(String args[]){
Integer i=new Integer(14);
//unboxing int type to Integer object
int a=i;
System.out.println(a);
}
Output-
14
- When dealing with collection framework classes-
In the below example, the object is automatically converted into the primitive type int and assigned to the variable a-
public static void main(String[] args) {
ArrayList < Integer > list = new ArrayList < > ();
//autoboxing
list.add(1);
list.add(2);
System.out.println("ArrayList is " + list);
// unboxing
//here the get() method returns the object at the index 0
int a = list.get(0);
System.out.println("Value at index 0 is " + a);
}
Output-
ArrayList is [1, 2]
Value at index 0 is 1
Auto-Unboxing :
Auto-Unboxing is nothing but converting Objects into primitive values automatically. With respect to java boxing and Unboxing occurs automatically, so JVM uses boxing and unboxing auto-boxing and auto-unboxing to store the primitive values in the list and to retrieve from the list.
import java.util.ArrayList;
import java.util.List;
public class IntegerExample {
public static void main(String[] args) {
// below is the process of converting int -> Integer
// also known as boxing
int i = 10; // primitive value
Integer intObj = new Integer(10);
// below is example for auto-boxing
List al = new ArrayList();
// syntax of adding a value to list
// al.add(Object e);
// but in place of Object we are able to store the int
// int is not stored as int but as Integer
//and then converted into Object class object
al.add(i);
// unboxing
intObj.intValue();
// on below line int stored as object is Un-boxed and became as int
// this is called as auto-unboxing
System.out.println(al.get(0));
}
}
Output-
10
Auto-Unboxing is nothing but converting Objects into primitive values automatically. With respect to java boxing and Unboxing occurs automatically, so JVM uses boxing and unboxing auto-boxing and auto-unboxing to store the primitive values in the list and to retrieve from the list.
import java.util.ArrayList;
import java.util.List;
public class IntegerExample {
public static void main(String[] args) {
// below is the process of converting int -> Integer
// also known as boxing
int i = 10; // primitive value
Integer intObj = new Integer(10);
// below is example for auto-boxing
List al = new ArrayList();
// syntax of adding a value to list
// al.add(Object e);
// but in place of Object we are able to store the int
// int is not stored as int but as Integer
//and then converted into Object class object
al.add(i);
// unboxing
intObj.intValue();
// on below line int stored as object is Un-boxed and became as int
// this is called as auto-unboxing
System.out.println(al.get(0));
}
}
Output-
10
Comments
Post a Comment