Four important concepts or principles in Java are
- Data encapsulation
- Data abstraction
- Inheritance
- Polymorphism
In this post, we will look at first two principles - Encapsulation and Abstraction.
General View
Data Encapsulation simply means wrapping relevant data together whereas Data abstraction means abstracting out the underneath logic and expose only the relevant part to the user. This is a very generic statement. We will get back to the what it actually means in Java language.
Data Encapsulation in Java
Java is an Object oriented programming language, That means everything in java is an object and these objects interact with each other to form an executing program. Classes are nothing but blueprints of Objects and these classes form the very basis of Data encapsulation.
Data encapsulation in its simple form means wrapping the relevant data in a class and controlling its access. This is also sometimes associated with another keyword - Data Hiding. When we design the class we essentially write encapsulation rules.Lets us go a little deeper to understand this concept -
- We achieve data encapsulation in java by using access modifiers - Public,Protected,default,Private.
This is also why it is referred to as data hiding. We hide the data and provide only desired access to it.
Note: Keywords encapsulation and data hiding are used synonymously everywhere. It should not be misunderstood that encapsulation is all about data hiding only. When we say encapsulation, emphasis should be on grouping or packaging or bundling related data and behavior together.
A very basic encapsulation example
public class Animal { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } }
Data Abstraction in Java
Data abstraction simply means generalizing something to hide the complex logic that goes underneath. Consider a very simple example of computer science - subtraction. If we have two variables "a" and "b" we simply say their subtraction is (a-b). But what is really happening behind the scenes? These variables are stored in main memory in binary format. The processor processes the subtract information which really takes place using two's complement method. Again in the processor, we have gates that actually carry out the procession. Even inside gates, there are pins and 1 and 0 simple means high and low signals.
Even though the process is so complex underneath a normal software developer developing some financial application need not know every small detail. This is because the concept has been abstracted out. This is what abstraction means in a general terminology.
- We achieve data abstraction in Java primarily by using Interfaces and abstract classes.
Lets us take an example to understand this function -
Let's say we have an interface Animal and it has a function makeSound(). There are two concrete classes Dog and Cat that implement this interface. These concrete classes have separate implementations of makeSound() function. Now let's say we have an animal(We get this from some external module). All user knows is that the object that it is receiving is some Animal and it is the user's responsibility to print the animal sound. One brute force way is to check the object received to identify it's type, then typecast it to that Animal type and then call makeSound() on it. But a neater way is to abstracts thing out. Use Animal as a polymorphic reference and call makeSound() on it. At runtime depending on what the real Object type is proper function will be invoked.
I would like to end this post by saying Data encapsulation(Data hiding) is wrapping and controlling access of logically related data in a class. One of the aspects of this is by using suitable access modifiers. Data abstraction, on the other hand, is generalizing the concept so that the underlying complex logic is hidden from the user. In java, this is achieved by using interfaces and abstract classes.
Encapsulation is combining related logic data (variables and methods) whereas Abstraction is hiding internal implementation details and expose only relevant details to the user. In a way you can Abstraction is achieved by Encapsulation.
A generic diagram to understand the difference is provided below. Note complex logic is on the circuit board which is encapsulated in a touchpad and a nice interface(buttons) is provided to abstract it out.