Built with Tutify.io

    AIStudyLabs

    Encapsulation and Access Modifiers

    Encapsulation and Access Modifiers

    Introduction

    In this lesson, we will explore the crucial Object-Oriented Programming (OOP) concept of encapsulation in Java. We will delve into how encapsulation enhances code reliability and security, examine different types of access modifiers such as public, private, and protected, and learn about getter and setter methods for implementing encapsulation in our classes. By the end of this lesson, you will understand how to effectively apply encapsulation principles to ensure better code management and protection against unintended access.

    Understanding Encapsulation in Java

    Encapsulation is one of the fundamental principles of OOP that strengthens data hiding and protects the internal state of an object from outside interference. In Java, this is accomplished by wrapping the data (variables) and code (methods) together into a single unit known as a class. The essence of encapsulation is to restrict direct access to some of an object's components, which is achieved through access modifiers.

    Why is Encapsulation Important?

    Access Modifiers in Java

    Access modifiers determine the visibility or access level of classes, methods, and variables. Java provides the following access modifiers:

    1. Public

    Members (variables or methods) declared as public can be accessed from any other class in the same package or in different packages.

    2. Private

    Members declared as private are accessible only within the class they are declared. This modifier restricts visibility from other classes, ensuring encapsulation.

    3. Protected

    Protected members are accessible within the same package and by subclasses in other packages.

    4. Default (Package-Private)

    When no access modifier is specified, the member is said to have default access, which is accessible only within its own package.

    Implementing Encapsulation

    To properly apply encapsulation in Java, we use getter and setter methods. Here’s how these work:

    Code Example: Implementing Encapsulation

    public class Person {
        /* Private variables */
        private String name;
        private int age;
    
        /* Getter method for name */
        public String getName() {
            return name;
        }
    
        /* Setter method for name */
        public void setName(String name) {
            this.name = name;
        }
    
        /* Getter method for age */
        public int getAge() {
            return age;
        }
    
        /* Setter method for age */
        public void setAge(int age) {
            if (age > 0) { // Basic validation
                this.age = age;
            } else {
                System.out.println("Please enter a valid age");
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Person person = new Person();
            person.setName("Alice");
            person.setAge(25);
    
            System.out.println("Name: " + person.getName()); // Output: Name: Alice
            System.out.println("Age: " + person.getAge());   // Output: Age: 25
        }
    }
    

    Program Explanation

    In the code above, we define a Person class with private attributes name and age. Getters and setters help manage these attributes securely. The setAge method includes a validation check to ensure that age cannot be set to a negative number.

    Output:

    Name: Alice
    Age: 25
    

    Applying Encapsulation Principles for Code Security

    By utilizing encapsulation effectively, we ensure that:

    Summary / Key Takeaways

    In this lesson on Encapsulation and Access Modifiers, you have learned:

    Java Fundamentals For Beginners — Object-Oriented Programming (OOP) Concepts

    Introduction to Java

    Java Basics

    Java Basic Programs

    Understanding Java Classes and Methods

    Working with Strings

    Object-Oriented Programming (OOP) Concepts

    Exception & Error Handling

    File Handling (I/O Operations)

    Java Packages and Libraries

    Best Practices in Java Programming

    Building Simple Console-Based Applications

    Moving Forward: Advanced Topics

    Course Review and Final Project

    Tutorial Not Found