Sharing Knlowledge With The World…

Month: March 2015

Abstraction in object-oriented programming

Abstraction in object-oriented programming

Abstraction came from the Latin word abs, meaning ‘away’ and trahere, meaning ‘to draw’. So we can define Abstraction in object-oriented programming language as a process of removing or taking away the characteristics from something (object) in order to reduce it to a set of essential characteristics.
Through the Abstraction in object-oriented programming, a programmer shows only the relevant data of an object and omitted all unwanted details of an object in order to reduce complexity and increase efficiency.
In the process of abstraction in object-oriented programming, the programmer tries to ensure that the entity is named in a manner that will make sense and that it will have all the relevant aspects included and none of the extraneous ones.
If we try to describe the process of abstraction in real world scenario then it might work like this:

You (the object) are going to receive your father’s friend from railway station. You two never met to each other. So you would take his phone number fron your father and call him when the train arrives.
On the phone you will tell him that “I am wearing white T-shirt and blue jeans and standing near the exit gate”. Means you will tell him the colour of your clothes and your location so he can identify and loacte you.This is all data that will help the procedure (finding you) work smoothly.

You should include all that information. On the other hand, there are a lot of bits of information about you that aren’t relevant to this situation like your age, your pan card number, your driving licence number which might be must information in some other scenario (like opening a bank account). However, since entities may have any number of abstractions, you may get to use them in another procedure in the future.

Continue Reading

Encapsulation in object-oriented language

Encapsulation

Encapsulation in object-oriented language or in Java is packing of data and function in to single component which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code.
Encapsulation in object-oriented language can also be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. (Through functions, which are exposed to outer world.)
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
Example:
public class UserPin {
private int pin;
public void setPin (int pin){
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from db and return back
}
}

Encapsulation in object-oriented language makes sure that the user of the class would be unaware of how class stores its data. Also it makes sure that user of the class do no need to change any of their code if there is any change in the class.
As in the above code example we store the ‘PIN’ of the user as integer but say, due to security reason we have to encrypt the ‘PIN’ and then store the encrypted ‘PIN’. And the algorithm that we use for encryption requires ‘PIN’ as String.
public class UserPin {
private int pin;
public void setPin (int pin){
//Convertin pin from int to String
//Encrytpt the PIN
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from database
//Converting back to int
//Returning the pin

}
}
As we saw there is no change in the signature of the functions so the user of the class do not have to change his code.
Also we can implement the security layer as the user access the field through the function (known as getter and setter).
public class UserPin {
private int pin;
public void setPin (int pin){
//Validate the value of the PIN
//Convertin pin from int to String
//Encrytpt the PIN
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from database
//Converting back to int
//Returning the pin

}
}
The fields can be made read-only (If we don’t define setter methods in the class) or write-only (If we don’t define the getter methods in the class).

The whole idea behind encapsulation is to hide the implementation details from users. That’s why encapsulation is known as data hiding.

The idea of encapsulation in object-oriented language is “don’t tell me how you do it; just do it.”

Continue Reading
PageLines