Internal stuff of inner classes
internal stuff of inner classes,internal stuff in inner classes in java,what is inner class,how inner class works,how inner classs works with example Inner Classes: inner class is a class written inside another class.use of inner class is to protect the data not available to others means providing security for the class at that time we go for inner classes
Example: Best example for the inner classes
"Bank Account: providing the security for calculating intrest of the total amount deposited in bank"
Observe the following program:
import java.io.*;
class BankAct
{
private double balance;
BankAct(double b)
{
balance = b;
}
void contact(double r)throws IOException
{
int i=0;
do{
System.out.print("Enter password:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String pwd = br.readLine();
if(pwd.equals("naga123"))
{
Interest in = new Interest(r);
in.calculateInterest();
return; //or System.exit(0);
}
else
{
if(i==2)
{
System.out.println("your accout has been blocked");
return;
}
else
{
System.out.println("Sorry, you are not an authorized person");
i++;
}
}
}while(i<=2);
}
private class Interest //this is an inner class reside in security area
{
private double rate;
Interest(double r)
{
rate = r;
}
void calculateInterest()
{
double interest = balance*rate/100;
balance +=interest;
System.out.println("Updated Balance= "+balance);
}
}
}
class InnerClassBankActDemo
{
public static void main(String[] args)throws IOException
{
BankAct act = new BankAct(10000);
act.contact(9.5);
}
}
No comments:
Post a Comment