Selenium Webdriver Day 3 : Abstraction and Encapsulation

Abstraction: In OOPS abstraction means hiding the implementation details from the end user or third party application like Paytm. In java, abstraction can be performed using Access Specifier(public, private, protected, default). We have already learned about access specifier in the previous article.
The example in real life: Take a scenario where user visiting e-commerce site like Paytm, netbankingMyntra etc, and after selecting an item he goes for the payment process into his bank net banking. Now in the case of an online transaction, there must be some method defined as public inside bank system so e-commerce website can complete their transaction. Also, all the other methods must be declared as private or protected so that outside user can not access their code just by using inheritance property.
By using abstraction property bank system secure their code.
Have a look into below code:
public class Account {
private float amount=10000.f;
private String name="Sachin";
//method for third party use
public void deduct_amount(float amt){
payment(amt);
}
//method for deduct amount calculation
private void payment(float amt){
amount=amount-amt;
}
}
In above example as you can see all the only one method is defined as public(deduct_amount) and as we have discussed we allowed accessibility of this method to every user so they can use this. All the other methods and variables are defined as private so no one can access that,
Encapsulation : Encapsulation means wrapping up data and methods together into a single unit.
Example :Look a the code below :
public class Account {
private float amount=10000.f;
private String name="Sachin";
//method for third party use
public void deduct_amount(float amt){
payment(amt);
}
//method for deduct amount calculation
private void payment(float amt){
amount=amount-amt;
some other class to save user data into database
SaveData d=new SaveData();
d.saveamount(user,amount);
}
}
In the payment() method we have defined a variable (amount), also we are creating an object and accessing method saveamount() using this method. Now if you look closely we are using this inside the single methods and this thing is known as encapsulation.

Popular posts from this blog

Selenium Webdriver Day 4 : Inheritance

Selenium Webdriver Day 1 : Class and object

Python Programming - Log file is not capturing the data?