Abstraction and Encapsulation in java
Encapsulation is mainly used to bind the data members within the class.
Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
The Java Bean class is the example of fully encapsulated class.
Advantage of Encapsulation :
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 type of a field and users of the class do not need to change any of their code.
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.
Abstraction is the process where you “throw-away” unnecessary details from an entity you plan to capture/represent in your design and keep only the properties of the entity that are relevant to your domain.
For getting a abstraction we need to use a keyword abstract .abstract keyword use with class. Which indicate that class is incomplete. Abstract class contains abstract methods(incomplete methods) and complete methods.
Difference between Encapsulation and Abstraction:
Abstraction: Abstraction means hiding the internal details and just exposing the functionality. For Example:- When user is using send SMS application he doesn’t know internal working of send SMS function but he is using that functionality without knowing internal details.
Encapsulation: Encapsulation means wrapping relevant data in a class and controlling its access. When we design the class we essentially write encapsulation rules. We achieve data encapsulation in java by using access modifiers — Public, Protected, default, Private. Java training center bangalore
We define functions or variables with some access modifier to control the scope that can be used or accessed by the user. For Example: Class is a example of Encapsulation.
Abstraction — hiding implementation.
encapsulation — hiding data
Real Time use of Encapsulation and Abstraction in Java Project:
It’s provides ability to simulate real-world event much more effectively. We can provide the solution of real world problem if we are using this concept.
It’s makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
It’s provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.
Strong Benefits of Encapsulation :
Encapsulation is minimizing what you expose to the user of your code. That “user” may be the rest of your code, or whoever uses the code you publish.
Strong Benefits of Abstraction :
When you change your code that implements an abstraction, the user of the abstraction doesn’t have to change their code. As long as the abstraction doesn’t change, the users won’t have to change their code.
When you write code that uses an abstraction, you can write code once that will be reusable against any new code that implements that abstraction. You can write less code to do more.
This is the basic concept about the encapsulation and abstraction . To get the more knowloedge on java join infocampus for java training.
4 days free demo classes are provided. For java course classes are available on flexible timing on weekend and weekdays. To get the good knowledge on java/j2ee attend classes in Infocampus Software Training Institute.
Learn Core and Advance Java in easiest way.
Contact at: 97380001024 or for more details on Advance java training visit at :http://www.infocampus.co.in/java-training-bangalore.html