Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lana-20/oop-encapsulation
Encapsulation binds together the code and data in a single unit of work (a class) and acts as a defensive shield that doesn’t allow the external code to access this data directly.
https://github.com/lana-20/oop-encapsulation
data-hiding encapsulation encapsulation-protocol oop oop-principles oops oops-in-java oops-in-python
Last synced: about 1 month ago
JSON representation
Encapsulation binds together the code and data in a single unit of work (a class) and acts as a defensive shield that doesn’t allow the external code to access this data directly.
- Host: GitHub
- URL: https://github.com/lana-20/oop-encapsulation
- Owner: lana-20
- Created: 2023-02-08T00:33:19.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-08T03:28:10.000Z (almost 2 years ago)
- Last Synced: 2024-11-08T02:49:37.047Z (3 months ago)
- Topics: data-hiding, encapsulation, encapsulation-protocol, oop, oop-principles, oops, oops-in-java, oops-in-python
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Encapsulation
_Encapsulation is one of the core concepts of Object Oriented Programming (OOP)._ Mainly, encapsulation binds together the code and data in a single unit of work (a class) and acts as a defensive shield that doesn’t allow the external code to access this data directly. _It is the technique of hiding the object state from the outer world and exposing a set of
public
methods for accessing this state. When each object keeps its stateprivate
inside a class, encapsulation is achieved. This is why encapsulation is also known as the **data-hiding** mechanism._The code that takes advantage of encapsulation is:
- [x] loosely coupled (eg., I can change the names of the class variables without breaking the client code)
- [x] reusable
- [x] secure (the client is not aware of how data is manipulated inside the class)
- [x] easy to test (it’s easier to test methods than fields)In Java, encapsulation can be achieved via the _access modifiers/specifiers_,
public
,private
, andprotected
.Everything is
public
by default.[In Python](https://github.com/lana-20/encapsulation-python):
* To make fields/variables and methodsprotected
, prefix them with a single underscore '_'. Eg.,_field
ordef _method()
.
* To make fields/variables and methodsprivate
, prefix them with a dunder '__'. Eg.,__field
ordef __method()
.Commonly, when on object manages its own state, its state is declared via
private
variables and is accessed and/or modified viapublic
methods. For example, aCat
class can have its own state represented by fields, such asmood
,hungry
, andenergy
. While the code external to theCat
class cannot modify any of these fields directly, it can callpublic
methods, such asplay()
,feed()
andsleep()
that modify theCat
state internally. TheCat
class may also haveprivate
methods that are not accessible outside the class, such asmeow()
.This is encapsulation.
[Java code sample](https://github.com/lana-20/oop-encapsulation/blob/main/java-class-cat):
public class Cat {
private int mood = 50;
private int hungry = 50;
private int energy = 50;public void sleep() {
System.out.println(“Sleep …”);
energy ++;
hungry ++;
}public void play() {
System.out.println(“Play …”);
mood ++;
energy --;
meow();
}public void feed() {
System.out.println(“Feed …”);
hungry --;
mood ++;
meow();
}private void meow() {
System.out.println(“Meow …”);
}public int getMood() {
return mood;
}public int getHungry() {
return hungry;
}public int getEnergy() {
return energy;
}
}The only way to modify the state is via the public methods, play(), feed(), and sleep().
public static void main(String[] args) {
Cat cat = new Cat();
cat.feed();
cat.play();
cat.feed();
cat.sleep();System.out.println(“Energy: ” + cat.getEnergy());
System.out.println(“Mood: ” + cat.getMood());
System.out.println(“Hungry: ” + cat.getHungry());
}The output is as follows:
Feed …Meow!Play …Meow!Feed …Meow!Sleep …
Energy: 50
Mood: 53
Hungry: 49Here's the Python equivalent of the Java code, using Python property decorators to demonstrate encapsulation in Object Oriented Programming:
class Cat:
def __init__(self):
self._mood = 50
self._hungry = 50
self._energy = 50def sleep(self):
print("Sleep…")
self._energy += 1
self._hungry += 1def play(self):
print("Play…")
self._mood += 1
self._energy -= 1
self._meow()def feed(self):
print("Feed…")
self._hungry -= 1
self._mood += 1
self._meow()def _meow(self):
print("Meow…")@property
def mood(self):
return self._mood@property
def hungry(self):
return self._hungry@property
def energy(self):
return self._energyif __name__ == "__main__":
cat = Cat()cat.feed()
cat.play()
cat.feed()
cat.sleep()print("Energy:", cat.energy)
print("Mood:", cat.mood)
print("Hungry:", cat.hungry)The output is as follows:
Feed…
Meow…
Play…
Meow…
Feed…
Meow…
Sleep…
Energy: 51
Mood: 52
Hungry: 52In this implementation, the
mood
,hungry
, andenergy
fields are declared as protected using the leading underscore notation. Theget_mood()
,get_hungry()
, andget_energy()
methods are replaced with Python property getters (declared using the @property decorator).This way, the internal state of the
Cat
object can be accessed using the properties (eg.,cat.energy
), but the underlying fields cannot be directly modified.