Download presentation
Presentation is loading. Please wait.
1
Encapsulation
2
Encapsulation Encapsulation is in used in JAVA to hide the implementations details from user. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. However if we setup public getter and setter methods to update and read the private data fields then the outside class can access those private data fields via public methods.
3
Encapsulation This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That’s why encapsulation is known as data hiding
4
Encapsulation Example
Here is a sample code to demonstrate Encapsulation in java, Fruit class has all related data like name, taste, color.. etc and behavior like calculateCost in a single unit.
5
Encapsulation Example package com.beingjavaguys.core;
public class Fruit { private String name; private String price; private String taste; private Fruit(String name, String price, String taste) { this.name = name; this.price = price; this.taste = taste; }
6
Encapsulation Example public void calculateCost() { }
} public String getName() { return name; } public void setName(String name) { this.name = name; public String getPrice() { return price; public void setPrice(String price) { this.price = price;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.