简体   繁体   English

抽象如何帮助隐藏 Java 中的实现细节?

[英]How does abstraction help in hiding the implementation details in Java?

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 hides the internal details.另一种方式,它只向用户显示重要的东西,隐藏内部细节。 So below is an example where an abstract class is made and abstract methods are overridden.所以下面是一个创建抽象类并覆盖抽象方法的示例。 But the thing i didn't understand is how it is hiding the implementation details?但我不明白的是它是如何隐藏实现细节的?

abstract class Bank
{    
   abstract int getRateOfInterest();    
} 

class SBI extends Bank
{    
 int getRateOfInterest()
  {
   return 7;
   }    
  }

class PNB extends Bank  
{    
 int getRateOfInterest()
   { 
    return 8;
   }    
 }    

class TestBank{    
public static void main(String args[])
{    
 Bank b;   
 b=new SBI();  
 System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
 b=new PNB();  
 System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
 }
 }     

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 剩下的就是实现 (以及实现细节),具体是: PNBSBI

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. 想象一下,您有一个由BankComparisonEngine类表示的银行比较引擎。 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: 它将仅以Bank抽象类)作为参数,然后获取其利率并将其保存到内部数据库中,如下所示:

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). 好吧, BankComparisonEngine不知道具体实施Bank getRateOfInterest()方法是如何工作的(即:实现了PNB.getRateOfInterest()SBI.getRateOfInterest() )。 It only knows it is a method that returns an int (and that it should return an interest rate). 它只知道这是一个返回int的方法(并且应该返回一个利率)。 The implementation details are hidden inside the concrete classes that extend abstract class Bank . 实现细节隐藏在扩展abstract class Bank的具体类内部。

When a client has to use your object, the client need not import your class or have your class definition in his jar, he/she can just import the abstract class or interface and accept your object as an argument, like explained in bank example.当客户必须使用您的对象时,客户不需要导入您的类或在他的 jar 中包含您的类定义,他/她只需导入抽象类或接口并接受您的对象作为参数,如银行示例中所述。 So client will still be able to use your object with parent reference but can't actually see (or need) the functional implementation of the class.因此客户端仍然可以使用带有父引用的对象,但实际上无法看到(或需要)类的功能实现。 Hope this cleared your doubt.希望这能消除您的疑虑。

谢谢 Deepak S,非常好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM