Gadgets

Saturday, 23 February 2013

Inner class in java with example


Inner class: -
Inner class is a class written in another class. It is a security mechanism. We can’t touch the inner class directly. Only authorization can use it. We must use private to inner class. We can’t create object directly to inner class.
Example program: -
public class BankAccount {
public static void main(String a[])
{
 Withdraw w=new Withdraw(); 
  }
}
class Bank
{
    private class Account   //inner class
    {
        int bal=0;
        Account(int bal)
        {
            this.bal=bal;
        }
    }
public void withdraw()
{
    Account a1=new Account(5000);
    int amt=200;
    a1.bal=a1.bal-amt;
    System.out.println("withdraw ammount  : "+amt );
    System.out.println("available bal : "+a1.bal);
}
}
class Withdraw
{
    Withdraw()
    {
        Bank b=new Bank();
        b.withdraw();
    }
}


No comments:

Post a Comment