Skip to main content

Chapter - 5:- What is Encapsulation in java

Encapsulation in java 


 Encapsulation:

  • The process of combining many elements into a single entity is called Encapsulation.
  • In Another Word:- 
  • The process of combining data member and member function into a single entity like class is called Data encapsulation.
  • It is an important features of object oriented programming.
  • It is used to prevent the direct accessibility of data member and member function and this is done by using access specifier public,private and protected.

 Access Specifier

I have already discuss in previous chapter in details what is access specifiers and some important  example. ACCESS SPECIFIERS 

1.It is a keyword which is used to provide accessibility of data member(variable) and member function(function) of a class.
2.It is also called access modifier.

Types of Access Specifier

There are Four types of access specifier.
Default     Public        Private         Protected

Default :- 

  • The class is only accessible by classes in the same package. This is used when you don't specify a specifiers,  You will learn more about packages in the Packages chapter.

Public   :-  

  • It has no Security, that means public members can be provided to the interface to the external world so can be access from any where in the program.
Private   :- 
  • It is most important and secure members(private) can be access only inside the class or type of access out-side the class.
  • To hide the data members input the in private area

 Protected:- 
  • It's less secure by private, protected members can be access by class member and by the members of derived class, that means we can say that protected members can be access inside the class and relatives class.
  • Protected is used in the case of inheritance.

An Example of Encapsulation : 


// Example 1: Find Simple Interest
class Encapsulation{
  Scanner sc= new Scanner(System.in);
  //Data Member;
  float p;
  float r;
  float t;
  float si;
  // member function
  void getsi(){

    System.out.println("Enter Principal Value:- ");
    p=sc.nextFloat();
    System.out.println("Enter Time Value:- ");
    t=sc.nextFloat();
    System.out.println("Enter Rate Value:- ");
    r=sc.nextFloat();
 

  }
  void siprocess(){
    si=(p*r*t)/100;

  }
  void print(){
    System.out.println("Your Simple Intrest Value:- "+si);
  }
  public static void main(String[] args){
  Encapsulation obj= new Encapsulation();     // create an instance objects
  obj.getsi();
  obj.siprocess();
  obj.print();
  }
}




******Output*********


PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Encapsulation
Enter Principal Value:-
1000
Enter Time Value:-
5
Enter Rate Value:-
6
Your Simple Intrest Value:- 300.0



I hope this is simple example for your better understanding of Encapsulation. Don't forget follow me. 




Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...