PRINCIPLE OF OOPs
Object-oriented programming has the following
principles
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
ENCAPSULATION :
● The process of binding the state(attributes/fields)
and behaviour of an object together is known as
encapsulation.
● We can achieve encapsulation in java with the help
of the class, class has both state and behaviour of an
object.
ADVANTAGE OF ENCAPSULATION :
ADVANTAGE OF ENCAPSULATION :
By using encapsulation we can achieve
data hiding.
DATA HIDING :
● It is a process of restricting the direct access of data
members of an object and provides indirect secured
access of data members via methods of the same object is
known as data hiding.
● Data hiding helps to verify and validate the data before
storing and modifying it.
STEPS TO ACHIEVE DATA HIDING
STEPS TO ACHIEVE DATA HIDING :
STEP 1: Prefix data members of a class with the private modifier.
STEP 2: Design a getter and setter method.
PRIVATE MODIFIER :
● private is an access modifier.
● private is a class-level modifier.
● If the members of the class are prefixed with a private
modifier then we can access that member only within the class.
NOTE :
Data hiding can be achieved with the help of a private modifier.
GETTER METHOD :
● The getter method is used to fetch the data.
● The return type of the getter method is the type of the hidden
value.
SETTER METHOD :
● The setter method is used to update or modify the data.
● The return type of the setter method is always void.
NOTE :
The validation and verification can be done in this method before
storing the data and before reading private data members.
NOTE :
● If you want to make your hidden data member-only readable then
create only the getter method.
● If you want to make your hidden data member-only modifiable then
create only the setter method.
● If you want to make your hidden data member both readable and
modifiable then create both getter and setter methods.
● If you want to make your hidden data member neither readable and
nor modifiable then don’t create a getter and setter method.
ADVANTAGES :
● Provides security to the data members.
● We can verify and validate the data before
modifying it.
● We can make the data member of the class to
➔ Only readable
➔ Only modifiable
➔ Both readable and modifiable
➔ Neither readable nor modifiable