Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java Encapsulation
Encapsulation means hiding something in terms of OOP concept. In java if we hide some variable from outside class then it will be called encapsulation.
In plain words capsules are used to hide drugs(medicine) so we don’t feel bad while taking to cure any disease, not exactly but similarly encapsulation hides some variables in class.
Encapsulation in Java
In java we can achieve encapsulation in class by hiding variables from outside word, we can do this by making variable private and then exposing it to the world by getter and setter methods.
Java Encapsulation Example
public class Clock { //hidden variable from outside class private String time; public String getTime() { return time; } public void setTime(String time) { this.time = time; } } class DemoClock { public static void main(String[] args) { Clock clock = new Clock(); clock.setTime("20:00"); String time = clock.getTime(); System.out.println("My encapsulated time is: "+time); } }
Output:
My encapsulated time is: 20:00
Pojo or Bean Class In Java
In java to save any information we create bean or pojo classes, these classes are good example of encapsulation where all variables may will be private and only way to update or access is getter and setter methods for variables in pojo or bean class.
In above example of Clock class will be called a bean or pojo class as well. Check below another example of encapsulation and pojo class:
Example:
class Alien { private String ageOfAlient; private String descriptionOfAlien; private String nameOfAlien; public String getNameOfAlien() { return nameOfAlien; } public void setNameOfAlien(String nameOfAlien) { this.nameOfAlien = nameOfAlien; } public String getAgeOfAlient() { return ageOfAlient; } public void setAgeOfAlient(String ageOfAlient) { this.ageOfAlient = ageOfAlient; } public String getDescriptionOfAlien() { return descriptionOfAlien; } public void setDescriptionOfAlien(String descriptionOfAlien) { this.descriptionOfAlien = descriptionOfAlien; } }
References
Article from oracle.com on oops concepts:Â OOP