
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement the encapsulation concept in JShell in Java 9?
Java Shell (simply JShell) is a REPL interactive tool for learning the Java and prototyping Java code. It evaluates declarations, statements, and expressions as entered and immediately prints out the result and runs from the command-line.
Encapsulation is an important concept in Java to make sure that "sensitive" data has been hidden from users. To achieve this, we must declare a class variable as private and provides public access to get and set methods and update the value of a private variable.
In the below code snippet, we have implemented the Encapsulation concept for Employee class.
jshell> class Employee { ...> private String firstName; ...> private String lastName; ...> private String designation; ...> private String location; ...> public Employee(String firstName, String lastName, String designation, String location) { ...> this.firstName = firstName; ...> this.lastName = lastName; ...> this.designation = designation; ...> this.location = location; ...> } ...> public String getFirstName() { ...> return firstName; ...> } ...> public String getLastName() { ...> return lastName; ...> } ...> public String getJobDesignation() { ...> return designation; ...> } ...> public String getLocation() { ...> return location; ...> } ...> public String toString() { ...> return "Name = " + firstName + ", " + lastName + " | " + ...> "Job designation = " + designation + " | " + ...> "location = " + location + "."; ...> } ...> } | created class Employee
In the below code snippet, we have created an instance of Employee class, and it prints out a name, designation, and location.
jshell> Employee emp = new Employee("Jai", "Adithya", "Content Developer", "Hyderabad"); emp ==> Name = Jai, Adithya | Job designation = Content Developer | location = Hyderabad.
- Related Articles
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?\n
- How to implement a lambda expression in JShell in Java 9?
- How to implement integer type conversion in JShell in Java 9?
- How to implement a Set interface in JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement String utility and immutability in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- How can we implement a map in JShell in Java 9?
- How to debug JShell in Java 9?
- How to reset the JShell session in Java 9?
- JShell in Java 9?

Advertisements