Print This Article Post Comment Add To Favorites Email to Friends Ezine Ready

Why Encapsulation Is Required In Java

By: Infocampus Home | Computers-and-Technology


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 acieve 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 .






Article Source: http://www.eArticlesOnline.com

About the 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

Tags: , , ,

Please Rate this Article

 

Not yet Rated

Click the XML Icon Above to Receive Computers-and-Technology Articles Via RSS!

Recent Related Articles From Computers-and-Technology

  • Web Design Composition Curriculum For 2017-2018
    By: Infocampus | May 24th 2017
    On the off chance that you are showing your understudies 'old fashioned' techniques from that time (ex: XHTML, Dreamweaver, Photoshop) that is likened to showing them how to utilize . Truth be told, showing old website architecture techniques may even be more terrible than instructing understudies to utilize a , in light o ... Read

  • What Is The Importance Of Immutable Property For String Class
    By: Infocampus | May 24th 2017
    Java String is class that java developers have written and put into java API . It is present in java.lang package. So when we will try to modify, new object will be created. Read

  • Are Emailed Invoices As Good As The Digital Ones?
    By: Jason Legg | May 24th 2017
    In short this post will go through the various kinds of email invoicing. Read

  • How Business Structure Can Be Improved By Sms Termination India?
    By: renukatewani | May 23rd 2017
    SMS termination India is nothing but a trend setter in the world of business promotion. A direct communication can be established and maintained in between you and your customers by this text service. Read

  • An Overview Of Bulk Invoice Emailing
    By: Jeremy Brown | May 22nd 2017
    Among the fastest and simplest ways for businesses to save lots of profit these belt tensing times is to begin bulk invoice emailing. Read

  • Evaluating Some Important Facts Before Opting Box Build Assembly Services
    By: PrintedCircuits | May 22nd 2017
    An overall electrochemical assembly process embracing other processes such as enclosure fabrication, installation and routing of cable harnesses, and installation of sub-assemblies and components is collectively known as systems integration or a box build. It is an assembly work other than the printed circuit board (PCB) pr ... Read

  • How To Connect Database In Java?
    By: Infocampus | May 22nd 2017
    For connecting a database with the application 5 steps are needed as Register the driver class , Creating connection , Creating statement, Executing queries, Closing connection. Read

  • Prepare Your Homes For Summers With Ac Installation Or Repair In St Louis
    By: Sophia Hill | May 22nd 2017
    When it comes to AC or condenser repair in St Louis then hiring professional HVAC contractors is most important thing. Professional HVAC contractors must take care of your systems and ensure the long-lasting performance. For air condition repair or installation in St Louis, Ricotta Heating & Air is the company to call upon. Read

  • How To Use The Html5 Sectioning Elements
    By: Infocampus | May 20th 2017
    The Problem With Link
    Consistently, a huge number of Web engineers conjure the omnipotent to gap, segment and ring-fence their Web pages' substance. We utilize the to police substance, to keep unique lumps of data from caving in into each other.
    Read

  • How Hibernate Framework Works In Java
    By: Infocampus | May 20th 2017
    Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool. Read


Copyright © 2005-2015 eArticlesOnline, LLC - All Rights Reserved
Terms of Service | Privacy Policy