Professional Documents
Culture Documents
Threads allow a program to operate more efficiently by doing multiple things at the same time.
Threads can be used to perform complicated tasks in the background without interrupting the
main program.
Encapsulation:
Encapsulation in Java is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed with several medicines.
We can create a fully encapsulated class in Java by making all the data members of the class
private. Now we can use setter and getter methods to set and get the data in it.
By providing only a setter or getter method, you can make the class read-only or write-only. In
other words, you can skip the getter or setter methods.
It provides you with control over the data. Suppose you want to set the value of id which
should be greater than 100 only, you can write the logic inside the setter method. You can write
the logic not to store the negative numbers in the setter methods.
It is a way to achieve data hiding in Java because other classes will not be able to access the
data through the private data members.
The encapsulated class is easy to test. So, it is better for unit testing.
The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and
fast to create an encapsulated class in Java.
Exception Handling
It's a mechanism to handle the runtime errors so that the normal flow of the application can be
maintained.
The core advantage of exception handling is to maintain the normal flow of the application. An
exception normally disrupts the normal flow of the application; that is why we need to handle
exceptions.
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10. statement 10;
Suppose there are 10 statements in a Java program and an exception occurs at statement 5; the
rest of the code will not be executed, i.e., statements 6 to 10 will not be executed. However,
when we perform exception handling, the rest of the statements will be executed. That is why we
use exception handling in Java.