How does abstraction helps in hiding the implementation details in Java?

后端 未结 1 1750
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 17:12

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only important things to the user and hid

相关标签:
1条回答
  • 2021-02-06 17:39

    The abstraction in your code is the abstract class itself:

    abstract class Bank {    
       abstract int getRateOfInterest();    
    } 
    

    and the rest is the implementation (and the implementation details), specifically: classes PNB and SBI

    But the thing i didn't understand is how it is hiding the implementation details?

    Imagine you have a bank comparison engine, which is represented by the BankComparisonEngine class. It will just take a Bank (abstract class) as an argument, then get its interest rate and save it to its internal database, like so:

    class BankComparisonEngine {
      public void saveInterestRateOf(Bank bank) {
        int rate = bank.getRateOfInterest();
        // Save it somwehere to reuse later 
      }
    }
    

    How are the implementation details hidden exactly? Well, BankComparisonEngine does not know how getRateOfInterest() method of a concrete implementation of Bank works (that is: PNB.getRateOfInterest() or SBI.getRateOfInterest() is implemented). It only knows it is a method that returns an int (and that it should return an interest rate). The implementation details are hidden inside the concrete classes that extend abstract class Bank.

    0 讨论(0)
提交回复
热议问题