Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is Encapsulation? 

What is Encapsulation? 

What is Encapsulation? 

Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP) in Java. It refers to the concept of wrapping data (variables) and methods (functions) into a single unit or class and restricting direct access to some of the object’s components. This helps to protect the internal state of the object and ensures that it can only be modified in controlled ways.

Key Aspects of Encapsulation: #

Private Variables: Data members (variables) of a class are made private to prevent direct access from outside the class. This ensures that the internal state of the object is not exposed or modified directly.

Public Methods: Access to the private data is provided through public methods known as getters and setters. These methods allow controlled access to the internal state of the object.

Data Hiding: By making variables private and providing public methods for access, encapsulation hides the internal implementation details from the outside world. This helps in maintaining control over the data and how it is modified.

Increased Flexibility and Maintainability: Encapsulation allows the implementation of a class to be changed without affecting the classes that use it. The internal workings of the class can be modified as long as the public interface (methods) remains consistent.

Example of Encapsulation: #

class Person {

    // Private variables

    private String name;

    private int age;

    // Public constructor

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

    // Public getter for name

    public String getName() {

        return name;

    }

    // Public setter for name

    public void setName(String name) {

        this.name = name;

    }

    // Public getter for age

    public int getAge() {

        return age;

    }

    // Public setter for age

    public void setAge(int age) {

        if (age > 0) { // Ensuring age is positive

            this.age = age;

        }

    }

    // Method to display person details

    public void displayInfo() {

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

    }

}

public class Main {

    public static void main(String[] args) {

        // Creating a Person object

        Person person = new Person("Alice", 30);

        // Accessing private variables via public methods

        person.displayInfo();

        // Modifying data via setter

        person.setName("Bob");

        person.setAge(35);

        // Display updated information

        person.displayInfo();

        // Direct access to private variables is not allowed

        // person.name = "Charlie"; // Error: name has private access in Person

        // person.age = -5; // Error: age has private access in Person

    }

}

Explanation of the Example: #

Private Variables:

name and age are private, so they cannot be accessed directly from outside the Person class.

Public Methods:

getName() and setName(String name) provide controlled access to the name variable.

getAge() and setAge(int age) provide controlled access to the age variable, including validation to ensure that the age is positive.

Data Hiding:

The internal representation of Person is hidden from the user, who can only interact with the class through its public methods.

Controlled Access:

The setters include validation (e.g., setAge(int age) ensures the age is positive) to protect the integrity of the data.

Summary #

Encapsulation is a key OOP principle that combines data and methods into a single class and restricts direct access to some of the object’s components. By using private variables and providing public methods for access, encapsulation enhances data security, provides flexibility in code maintenance, and ensures that the internal state of objects is controlled and managed effectively.

Java
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
What are access modifiers?What is Abstraction? 
Table of Contents
  • Key Aspects of Encapsulation:
  • Example of Encapsulation:
  • Explanation of the Example:
  • Summary
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105