Java 2
Java 2
A Wrapper class in Java is a class whose object wraps or contains primitive data types. When
we create an object to a wrapper class, it contains a field and in this field, we can store
primitive data types. In other words, we can wrap a primitive value into a wrapper class
object. Let’s check on the wrapper classes in Java with examples:
Need of Wrapper Classes
There are certain needs for using the Wrapper class in Java as mentioned below:
1. They convert primitive data types into objects. Objects are needed if we wish to
modify the arguments passed into a method (because primitive types are passed by
value).
2. The classes in java.util package handles only objects and hence wrapper classes help
in this case also.
3. Data structures in the Collection framework, such as ArrayList and Vector, store only
objects (reference types) and not primitive types.
4. An object is needed to support synchronization in multithreading
Strings
. StringBuffer
StringBuffer is a peer class of String, it is mutable in nature and it is thread safe class , we
can use it when we have multi threaded environment and shared object of string buffer i.e,
used by mutiple thread. As it is thread safe so there is extra overhead, so it is mainly used for
multithreaded program.
3. StringBuilder
StringBuilder in Java represents an alternative to String and StringBuffer Class, as it creates a
mutable sequence of characters and it is not thread safe. It is used only within the thread ,
so there is no extra overhead , so it is mainly used for single threaded program
Pillar 1: Abstraction
Data Abstraction is the property by virtue of which only the essential details are displayed to
the user. The trivial or non-essential units are not displayed to the user. Ex: A car is viewed as
a car rather than its individual components.
Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object, ignoring the irrelevant details. The properties and behaviors of
an object differentiate it from other objects of similar type and also help in
classifying/grouping the object.
Consider a real-life example of a man driving a car. The man only knows that pressing the
accelerators will increase the car speed or applying brakes will stop the car, but he does not
know how on pressing the accelerator, the speed is actually increasing. He does not know
about the inner mechanism of the car or the implementation of the accelerators, brakes etc.
in the car. This is what abstraction is.
In Java, abstraction is achieved by interfaces and abstract classes. We can achieve 100%
abstraction using interfaces.
Pillar 2: Encapsulation
It is defined as the wrapping up of data under a single unit. It is the mechanism that binds
together the code and the data it manipulates. Another way to think about encapsulation is
that it is a protective shield that prevents the data from being accessed by the code outside
this shield.
Technically, in encapsulation, the variables or the data in a class is hidden from any
other class and can be accessed only through any member function of the class in
which they are declared.
In encapsulation, the data in a class is hidden from other classes, which is similar to
what data-hiding does. So, the terms “encapsulation” and “data-hiding” are used
interchangeably.
Encapsulation can be achieved by declaring all the variables in a class as private and
writing public methods in the class to set and get the values of the variables.
Pillar 3: Inheritance
Inheritance is an important pillar of OOP (Object Oriented Programming). It is the
mechanism in Java by which one class is allowed to inherit the features (fields and methods)
of another class. We are achieving inheritance by using extends keyword. Inheritance is also
known as “is-a” relationship.
Let us discuss some frequently used important terminologies:
Superclass: The class whose features are inherited is known as superclass (also
known as base or parent class).
Subclass: The class that inherits the other class is known as subclass (also known as
derived or extended or child class). The subclass can add its own fields and methods
in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
Pillar 4: Polymorphism
It refers to the ability of object-oriented programming languages to differentiate between
entities with the same name efficiently. This is done by Java with the help of the signature
and declaration of these entities. The ability to appear in many forms is called
polymorphism.
Cannot be instantiated;
Specifies a set of methods a
contains both abstract (without
class must implement;
implementation) and concrete
methods are abstract by
methods (with
default.
Definition implementation)
Abstraction Encapsulation
In abstraction, problems are solved at the While in encapsulation, problems are solved
design or interface level. at the implementation level.
The objects that help to perform Whereas the objects that result in
abstraction are encapsulated. encapsulation need not be abstracted.
data hiding.
Abstraction focuses on “what” the object Encapsulation focuses on “How” the object
does . does it.
THREADS
ultithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU. Each part of such program is called a thread. So,
threads are light-weight processes within a process.
Threads can be created by using two mechanisms :
1. Extending the Thread class
2. Implementing the Runnable Interface
Thread creation by extending the Thread class
We create a class that extends the java.lang.Thread class. This class overrides the run()
method available in the Thread class. A thread begins its life inside run() method. We create
an object of our new class and call start() method to start the execution of a thread. Start()
invokes the run() method on the Thread object.