programming, technology

How to think in objects and classes – Java — Part 2


Encapsulation (Data Hiding)

Hello there programmer ๐Ÿ˜€

If you haven’t taken a look at the Part 1, then please do before continuing.

In this article, we will talk about one important concept of OOP. Encapsulation. This concept is also known as data hiding. This concept may appear meaningless to you I know. It was so for me in the beginning too but with time you will understand why it is important.

What is it?
You know we don’t like “book” definition of terms. We like to understand in simple terms and in practical terms what the book says.
Encapsulation means, putting some restrictions on attributes of a class so that functions and classes outside that class, cannot easily access. You might be wondering “Why should I make life hard for myself?” but I promise this is worth it.

How to see it
From our previous article, we remember the Person class from which we got the person object. We could just get the person’s first name by directly accessing it this way: person.first_name. Now, to encapsulate means, to prevent this direct access. In real life, let’s say you want to know a person’s blood pressure, weight, height (Let’s assume they haven’t memorized all that). You’ll need to use some equipment, you’ll need to ask them to sit, stand etc. Even if it’s their name, you can’t just read it off their forehead, you’ll have to ask them. In getting any detail, you have to do an action. That’s how to see encapsulation. You have to access variables through “actions” (methods), not directly.

Down to business
So how do we hide data? Notice how we preceed variables and methods with “public”, we’ll change that of the variables to “private”. Let’s take a look at it.


package javaapplication1;

public class Person {
 private String first_name;
 private String surname;
 private String location;
 private int age;

 public void walk(){
 System.out.println("Walking...");
 }

 public void talk(String what_to_say){
 System.out.println(first_name+" says: "+what_to_say);
 }
}

These variables are directly accessible inside the class only. It’s a good programming practice to implement your logic in methods that are in the class than to implement it outside of the class while accessing variables inside the class. We will take a look at a practical example as we go on.

How do we get access to them then?
If you followed the advice up there, chances are you may not need to directly access an individual variable as it is. Let’s picture it this way. Instead trying to read a person’s first name off the ID on his chest you may ask him. He’ll then decide what to tell you. Either he gives you his full name, first name, last name or a short form. Some processing is done!

Now take a close look at our two classes. There’s been quiet a change. We will talk about all the changes. Keep your cool and don’t panic ๐Ÿ™‚


public class Person {
 private String first_name;
 private String surname;
 private String location;
 private int age;

 public Person(String first_name, String surname, String location, int age){
 this.first_name = first_name;
 this.surname = surname;
 this.location = location;
 this.age = age;
 }

 public String say_my_name(){
 return "My name is "+first_name+" "+surname+
". But you can just call me "+ first_name.substring(0,3);
 }

 public void walk(){
 System.out.println("Walking...");
 }

 public void talk(String what_to_say){
 System.out.println(first_name+" says: "+what_to_say);
 }
}

 


package javaapplication1;

public class JavaApplication1 {

public static void main(String[] args) {
Person person1 = new Person("Benard","Bush","New York", 28);

 //we've given person1 some details

 Person person2 = new Person("Samuel","Sanders","Detroit",24);

 //let's walk and talk
 person1.walk();
 person1.talk("Hi there.. what's your name");

 person2.talk(person2.say_my_name());
 person2.talk("You?");
 person1.talk(person1.say_my_name());
}

Quickly… constructors…

The most notable change in the first class is the method “public Person”. This is a special method. This method is called the constructor. This method is called any time an object is created. It can take arguments like other methods but this time around, you just pass the arguments to the class during creation of the object. (Check the object declaration in the JavaApplication1). This method must have the same name as the class.

Quickly… the “this” keyword

“this” simply means what it says. This! This object. The current object. Remember when we use to access variables outside the class? We did it this way, person1.first_name. This time, we are trying to distinguish between two variables with the same name. We want to say, this object’s first_name = the argument first_name.

Back to business

Now, we have a function that accesses the name of the person nicely. Whenever you want to know a person’s name, (in the main method, which is outside the Person class) you make them say it by calling the “say_my_name” method. This returns a string which is then parsed to the talk method. Done! Cool right?

Why? Why all this?

Let’s assume we’re not encapsulating our variables. We’re just accessing them outside the class. We’ve used the variables in about three different classes and called it in so many methods. Now we want to change the variable’s name from “first_name” to “other_names”. We’ll have to go through every single class and make that change. We’ll have to go through every single method and make changes. We won’t have to do that with the encapsulation restriction.

Another example. If you’re working on a project with a colleague, their understanding and coding style may differ. It wouldn’t be advisable to let him access your variables and use in his logic. There maybe a lot of issues if you do any changes to your code or if his uses them wrongly.

Have a happy programming life ๐Ÿ™‚

In our next lesson, we’ll look at Inheritance and chip in class variables. ๐Ÿ˜‰

Advertisements

1 thought on “How to think in objects and classes – Java — Part 2”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s