As we know that, Abstraction is Hiding the complexity. Here we will learn that, to whom and what complexity we need to hide.
First thing which we need to know is what complexity we need to hide?
We need to hide the method implementations. Here we treat method implementations as a complexity. Why? we will see it soon.
To whom we need to hide? From the calling class, method or reference we need to hide those complexities.
Now for better understanding we will take a classic example that is ATM machine example which we studied so many times, when we read abstractions.
Let's suppose we are developing an application for ATM. As we know that in ATM we can do withdraw cash, check balance and change pin. So if any machine can do these three operations, can be an ATM machine.
As we know that in JAVA, operations are defined into methods. So if any class have these three methods, can be an ATM class.
Here we have made a contract for ATM class. Also we know that Interface is a contract for a class. So that we can make an Interface for ATM i.e.
1 2 3 4 5 6 7 | public interface Atm{ public boolean withdrawCash(Double amount); public Double checkBalance(); public boolean changePin(Integer oldPin, Integer newPin); } |
Now as we can see that here we have three methods,
1. withdrawCash
2. checkBalance
3. changePin
If any class Implements Atm interface, they have to override all these methods.
Ok, here we have achieved the abstraction. How?
Suppose we have have three ATMs, one is belongs to Bank of India, second is belongs to ICICI and third one belongs to PNB.
Now we will treat each bank as an Implementation class for ATM i.e.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class BankOfIndia implements Atm{ public boolean withdrawCash(Double amount){ // check the balance first then do withdraw oprations. return true; } public Double checkBalance(){ //Check the balance return null; } public boolean changePin(Integer oldPin, Integer newPin){ //Validate the old pin then change new pin return true; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Icici implements Atm{ public boolean withdrawCash(Double amount){ // check the balance first then do withdraw oprations. return true; } public Double checkBalance(){ //Check the balance return null; } public boolean changePin(Integer oldPin, Integer newPin){ //Validate the old pin then change new pin return true; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Pnb implements Atm{ public boolean withdrawCash(Double amount){ // check the balance first then do withdraw oprations. return true; } public Double checkBalance(){ //Check the balance return null; } public boolean changePin(Integer oldPin, Integer newPin){ //Validate the old pin then change new pin return true; } } |
Now these three classes have complexities which we need to hide. From Whom?
From the calling class, or method or reference.
Now according to my statement, we can assume that, why we need Interface here? when we call any method from the calling class or method or reference, we don't need to know the method implementation, what we need to know is only it's definition.
But, abstraction is not only for hiding the method implementation, or if I use more easy word, code of method. The complexity is not only the method's code. But complexity is to choose which classe's object I have to choose.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Machine{ private Atm choosedAtm; public Machine(Atm choosedAtm){ this.choosedAtm = choosedAtm; } public Atm getAtm(){ return choosedAtm; } } |
Suppose we have a Machine class, into machine class we have a constructor, which takes Atm interface as a parameter.
Now this Parameter don't know from which bank, the object is received. I mean to say,
it can be one of them:
1 2 3 | Machine m = new Machine(new BankOfIndia()); Machine m = new Machine(new Icici()); Machine m = new Machine(new Pnb()); |
So here Machine don't care from which class, reference of Atm is come, what he need to know is Atm have three methods which he need to operate.
Here we have hide the Complexity from the Machine class.
No comments:
Post a Comment