-1

Here in below mentioned code we have abstract class with abstract method and implemetation is present in child classes. and We can create object of child classes with referance var of parent class and can access the methods. So how we are hiding implementation here. Same thing can be done with normal class as well using method overriding. what we would acheive with the help of abstract classes and interfaces? What is the meaning of hiding implementation details?

// Code for abstract class


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()+" %");  
}
}

//code for non abstract class



public class Bank
{  
public int getRateOfInterest()
{
return 5;
}  
}

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()+" %");  
}
}

1 Answers1

0

Generally, an abstract class in Java is a template that stores the data members and methods that we use in a program. Abstraction in Java keeps the user from viewing complex code implementations and provides the user with the necessary information. And also it helps us to avoid duplication in code. See for more information and See why it abstraction is important