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:
and the rest is the implementation (and the implementation details), specifically: classes
PNB
andSBI
Imagine you have a bank comparison engine, which is represented by the
BankComparisonEngine
class. It will just take aBank
(abstract class) as an argument, then get its interest rate and save it to its internal database, like so:How are the implementation details hidden exactly? Well,
BankComparisonEngine
does not know howgetRateOfInterest()
method of a concrete implementation ofBank
works (that is:PNB.getRateOfInterest()
orSBI.getRateOfInterest()
is implemented). It only knows it is a method that returns anint
(and that it should return an interest rate). The implementation details are hidden inside the concrete classes that extendabstract class Bank
.