ALL >> Computer-Programming >> View Article
Why Encapsulation Is Required In Java
Introduction :
Encapsulation means binding the datamembers with in the class. For binding the datamembers members should be private.
Encapsulation is one of the four fundamental OOP concepts. The other three OOPs concept are inheritance, polymorphism, and abstraction.
What is Encapsulation ?
Encapsulation in Java is a mechanism of wrapping the code and data (variables)acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
How can we achieve the encapsulation ?
- Declare the variables of a class as private. Java /j2ee classes bangalore
-Provide public setter and getter methods to modify and view the variables values.
Why datamembers should be private ?
Private Datamembers are only accessible with in the class . Private members can’t be access outside the class.We can access the private members outside the class using getter and setter method.
Why we used Encapsulation ?
...
... - To make the fields of a class can be made read-only or write-only.
- A class can have total control over what is stored in its fields.
- The users of a class do not know how the class stores its data. A class can change the data Advanced java training in bangalore type of a field and users of the class do not need to change any of their code.
1.Objects encapsulate data and implementation details. To the outside world, an object is a black box that exhibits a certain behavior.
2.The behavior of this object is what which is useful for the external world or other objects.
3.An object exposes its behavior by means of public methods or functions.
4.The set of functions an object exposes to other objects or external world acts as the interface of the object.
More about Encapsulation :
- In Java , encapsulation is used to bind the data with its related functionalities. Here functionalities mean "methods" java course in bangalore and data means "variables"
So keep ing variable and methods in one place. That place is known as "class."
- Class is the base for encapsulation.
- With Encapsulation, we can hide (restrict access) to critical data members in our code, which improves security
-if a data member is declared "private", then it can only be accessed within the same class. No outside class can access data member (variable) of other class.
- However if we need to access these variables, we have to use public "getter" and "setter"methods.
-Setup public "getter" and "setter" method to update and read the private data field. This will allow data access from private class. Best core java training in bangalore
Getters Method and Setters Method :
In Java getters and setters are completely ordinary functions. The only thing that makes them getters or setters is convention. A getter for demo is called getDemo and the setter is called setDemo .
In the case of a boolean, the getter is called isDemo .
We can set as well as get value from variables so these are called setter and getter methods.
so declare variables as private to prevent from accessing directly using object of the class. and use get and set methods in java like
setVariableName() and getVariableName() to assign values and access variables .
Java training in bangalore
1. Purpose of Setter method is to set new value or assign new value to instance variable .
2. Method name should follow naming convention setVARIABLENAME().
3. It should accept some value as an argument. here method argument should be of type of variable.
4. It should have a statement to assign argument value to corresponding variable.
5. It does not have any return type. void should be the method return type.
6. In order to set some value to variable we need to call corresponding setter method by passing required value.
This is the short description about the Encapsulation . To get the more knowledge on java join Infocampus Software Training institute for Core & Advanced Java .
Author : Infocampus is one of the best institute for the java/j2ee classes bangalore.
It provides Advanced java training with the live project . In Infocampus 3 days free demo classes are available for java training . Learn OOPs concept , packages , multithreading , servlet , struts framework etc in Java Course. For the best core java training fully practical classes are available .
To book the seat for free demo class call at : 9738001024 or to get the more knowledge on java/j2ee classes enquire at : http://www.infocampus.co.in/java-training-bangalore.html
Add Comment
Computer Programming Articles
1. Reputation Management In The Digital Age: Protecting And Enhancing Your Law Firm’s ImageAuthor: jamewilliams
2. What Features Should I Look For In Wordpress Ecommerce Plugins?
Author: Rocket Press
3. Staying Updated With The Latest Gaming News
Author: Next Tech Plus
4. Game Development: Evolving Technologies And New Horizons
Author: Rick John
5. Why Svelte Is The Most In-demand Framework For Web Development?
Author: Why Svelte Is The Most In-Demand Framework For Web
6. Maximizing Ebay Sales With Maropost/neto And Pursuit Info Solutions
Author: rachelvander
7. The Importance Of Software Testing: Ensuring Quality In Development
Author: John Mathew
8. Sadhgurusilveroaks - The Best School In Nellore
Author: Sadhgurusilveroak
9. Website Development Using Kentico – Cloud First Headless Cms
Author: Website Development Using Kentico – Cloud First He
10. Shopify Experts In Melbourne: Elevate Your E-commerce With The Merchant Buddy
Author: themerchantbuddy
11. Web Development 3.0: Shaping The Future Of The Internet
Author: Backend Brains
12. Top College Erp In India Helps Colleges Operate More Efficiently
Author: CONTENT EDITOR FOR SAMPHIRE IT SOLUTIONS PVT LTD
13. "lcc Computer Education: Expert Java Coaching Center"
Author: Khushi Gill
14. Which Institute Is Best For Full Stack Developers In Bhopal?
Author: Shankar Singh
15. Micheal John
Author: micheal