DEV Community

Cover image for Three characteristics of OOP: Encapsulation
Yuehui Ruan
Yuehui Ruan

Posted on • Edited on

Three characteristics of OOP: Encapsulation

Three main features of OOP language.


PREFACE:
(you can just skip this section to Introduction XD )

Okay, Java and cpp are the language that I learn from my school, though this summer I receive an email from my professor that we "had better" to have some expreience on C. (And I use fews week to be acquainted with this. OMG)

And, we know those two languages are Object Oriented Programming languages. So they have three features that are different from Procedure-Oriented Language (like C, so far I have learnt).

Due to these three features have a high occurrence rate on interviews from many comps, I decide to summary my knowledge.

This blog is based on the language of JAVA, and you should know there are still difference amoung all OOP languages. And in this blog I will try to use "my word" and my thinking to describe what they are and why they exist, how to use them. Please correct me if there is any wrong in my blog. Thank you!


Introduction

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Encapsulation: What Why How

1. Defination (What):

Put the objects which have same features or, belongs to the same class together, and pack them well.

So you know in the company, there are lots of departments that led by different managers or supervisors. For example, we have IT_Department class and HR_Department class.

class HR_Department{

    private String Manager;
    private String[] Dep_Members;

    HR_Department(String manager_name){// constructor of HR_Dep
        Manager = manager_name;
    }

    private void employ(){
        System.out.println("Our company need to employ a new employee.");
    }
    private void dismissal(){
        System.out.println("We decide to dismissal someone...");
    }
}

class IT_Department{

    private String Manager;
    private String[] Dep_Members;

    IT_Department(String manager_name){// constructor of IT_Dep
        Manager = manager_name;
    }

    private void Access_Database(){
        System.out.println("Our IT Department can access database of our company.");
    }
    private void Power_outage(){
        System.out.println("IT Department decide to shut down the power for fixing problems.");
    }
}

class Google_Inc{
    private IT_Department it_Dep;
    private HR_Department hr_Dep;

    Google_Inc(String IT_Manager_Name,String HR_Manager_Name){// constructor of Google_Inc
        it_Dep = new IT_Department(IT_Manager_Name);
        hr_Dep = new HR_Department(HR_Manager_Name);

        System.out.println("Google Inc is established successfully!");
    }
}

public class Test {

    public static void main(String[] args) {
        // set up a company call newGoogle_Inc XD
        Google_Inc google_Inc = new Google_Inc("Jack", "Rose");
    }
}
Enter fullscreen mode Exit fullscreen mode

If you are CEO of this company, you have to employ some managers to help you manage these different departments, and of course you require any department work in perfect order and help each other
.

2. Significance (Why):

You can easily imagine that, how messy this scenario would be if we didn't have access to every piece of data in any class.

I believe, you can't stand it when HR employee turns off the power without checking in with THE IT department. (even I never work for any companys XD) It's dangerous and out of line anywhere.

So the existance of Encapsulation shows its importance right now. The example of company is also suitable for any program's, code's, data's management. It's dangerous while after you design a program, some of your users could access your data and edit them.

So for Java, we usually "pack" our CLASS (data) well through using Permission Modifier.
1.public
2.protected (default)
3.private (most used to modify class and variable, as you can see on previous code block)

Obviously, you can realize that, if you correctly use the
permission modifier, you can realize the advantages of your code:

1.Isolate important variables(vars) or functions(funcs)
2.Conveniently access vars or funcs
3.Increase the importance
4.Enhance the security

And of course, these advantages also cause the code being much more harder to access and leading you spending more steps to access them.

3. Implement of Encapsulation(HOW):

Before using them, you need to know the range of different modified vars or funcs that could be accessed.

Modify Same class Same package Sub class All class
private Yes
default Yes Yes Yes
protected Yes Yes Yes
public Yes Yes Yes Yes

In some way, protected equals to default.

Eg: You can not access manager name of HR department directly by assign =:
image

So, you need to set up some functions called "Getters" and "Setters":

class IT_Department{

    private String Manager;
    private String[] Dep_Members;

    IT_Department(String manager_name){// constructor of IT_Dep
        Manager = manager_name;
    }

    String getManager(){ // a classic getter function
        //return manager's name
        return Manager;
    }
    void setManager(String name){// a classic setter function
        //set name to manager's name
        this.Manager = name;
    }

    private void Access_Database(){
        System.out.println("Our IT Department can access database of our company.");
    }
    private void Power_outage(){
        System.out.println("IT Department decide to shut down the power for fixing problems.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Hence, you can realize, the implement of encapsulation is abstract in code. We prefer to call encapsulation a phenomenon or a feature. If you have a clear and strict frame of code, your codes will look so GOOD and COMFORTABLE.

In main function:

public static void main(String[] args) {
        // set up a company call newGoogle_Inc
        Google_Inc google_Inc = new Google_Inc("Jack", "Rose");

        String name = google_Inc.it_Dep.getManager(); // assign "Jack" to name.
        google_Inc.it_Dep.setManager("Ben");// assign "Ben" to IT Department's manager
    }
Enter fullscreen mode Exit fullscreen mode

Your advice and comments are the power of my moving forward. Thank you for your reading.

Never know how good you will be if you don't push yourself.
Ricky Ruan:

E-mail: yruan@umass.edu

Top comments (1)

Collapse
 
divide_r_conquer profile image
Yuehui Ruan

Welcome all the advice and correction! Thank you!