Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()+" %");    
 }
 }     
like image 727
Tarunjeet Singh Salh Avatar asked Sep 04 '17 16:09

Tarunjeet Singh Salh


People also ask

How does abstraction hide implementation?

Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

How does abstraction hide data?

It is defined as the process of hiding the internal implementation and keeping the complicated procedures hidden from the user. Only the required services or parts are displayed. This is usually achieved using 'abstract' class concept, and by implementing interfaces. Only the important details are highlighted.

How hide data in abstraction in Java?

In java it is achieved by using a keyword 'private' keyword and the process is called data hiding. It is used as security such that no internal data will be accessed without authentication. An unauthorized end user will not get access to internal data.

Why do we use abstraction in Java?

Abstraction in Java keeps the user from viewing complex code implementations and provides the user with necessary information. We cannot instantiate the abstract class in Java directly. Instead, we can subclass the abstract class.


1 Answers

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.

like image 134
syntagma Avatar answered Oct 09 '22 15:10

syntagma