Code Monkey home page Code Monkey logo

java-mod-2-encapsulation's Introduction

Encapsulation

Learning Goals

  • Review the definition of encapsulation
  • Learn how it is used in Java

What is Encapsulation?

As we saw in the last lesson, encapsulation is one of the four pillars of object-oriented programming. Encapsulation is a programming mechanism that wraps up data and its code acting on the data in a single unit and keeps it safe from outside interference. We can think about our classes and the instance variables that were encapsulated inside them using the private access modifier. We can think of this as data-hiding.

Encapsulation Example

Believe it or not, we actually already know all the tools to hide our data from other classes! We learned about access modifiers, accessor methods, and mutator methods all in the last module. As we said time and time again:

It is best practice for us to make our variables private and their values accessible through public methods (accessor and mutator methods).

But this just isn't a common practice - it is actually a rule of thumb when it comes to encapsulation! Let's go back to our Student class from the last module:

public class Student {
    private String firstName;
    private String lastName;
    private String major;

    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.major = "Undeclared";
    }
    
    public Student(String firstName, String lastName, String major) {
       this(firstName, lastName);
       this.major = major;
    }

    public String getFirstName() {
       return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public String getLastName() {
        return lastName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    public String getMajor() {
        return major;
    }
    
    public void setMajor(String major) {
        this.major = major;
    }
}

In our Student class, we are hiding the firstName, lastName, and major data points from other classes by restricting their access to private. Only methods within the Student class can truly modify their values. If other classes need access to these variables, then they have to go through the getters and setters defined and have public access. This allows the object to be in charge of its own state.

What Happens If We Don't Use Encapsulation?

If we were to make one of the variables public in a class, then it would open up doors for other classes to change the object's state! To better explain, let us look at an example that doesn't use encapsulation and has its variables set to public:

public class Dog {
    public String bark;
    
    public Dog() {
        this.bark = "Woof!";
    }
    
    public static void main(String[] args) {
        Dog snoopy = new Dog();
        snoopy.bark = "Meow!";
    }
}

The above code is valid but do we see what happened? Our Dog instance is now meowing instead of barking! The variable is no longer encapsulated within the Dog class and can now be modified through careless intent. This is why encapsulation is so important - it keeps the data hidden and safe.

java-mod-2-encapsulation's People

Contributors

kcleland0818 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.