Sunday, 13 October 2013

Program to demonstrate Multilevel Inheritence

//Program to demonstrate Multilevel Inheritence
//Program by Ahlam Ansari
class Person
{
    private String ownersName;
    public Person(String aName)
    {
        ownersName = aName;
    }
   
    String greeting()
    {
        System.out.println("Welcome!! ");
        return(ownersName);
    }
}

class BankAccount extends Person
{

    private int accountNumber;
    private float balance;

    public BankAccount(int anAccountNumber, String aName, float aBalance)
    {
        super(aName);
        accountNumber = anAccountNumber;
        balance = aBalance;
    }

    public void deposit(float anAmount)
    {
        if (anAmount>0.0)
            balance = balance + anAmount;
    }

    public void withdraw(float anAmount)
    {
        if ((anAmount>0.0) && (balance>anAmount))
            balance = balance - anAmount;
    }

    public float getBalance()
    {
        return balance;
    }
   
    public void updateBalance(float aBalance)
    {
        balance= aBalance;
    }

}

class SavingAccount extends BankAccount
{
    private float saveLimit;

    public SavingAccount(int anAccountNumber, String aName, float aLimit, float aBalance)
    {
        super(anAccountNumber, aName, aBalance);
        saveLimit = aLimit;
    }

    public void withdraw(float anAmount)
    {
        float aBalance;
        if ((anAmount>0.0) && (getBalance()>anAmount)&&(getBalance()-anAmount>=saveLimit))
        {
            aBalance = getBalance() - anAmount;
            updateBalance(aBalance);
        }
        else
            System.out.println("Cannot withdraw as balance is insufficient");
    }

}

class MultiLevel
{
    public static void main(String ahlam[])
    {
         SavingAccount anAccount = new SavingAccount(8801, "Ahlam Ansari", 10000.0f , 50000.0f);
         anAccount.deposit(57890.0f);
         System.out.println(anAccount.greeting()+" your Current Balance is Rs." +anAccount.getBalance()+"/-");
         anAccount.withdraw(11890.0f);
         System.out.println(anAccount.greeting()+" your Current Balance is Rs." +anAccount.getBalance()+"/-");
    }
}

/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MultiLevel.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MultiLevel
Welcome!!
Ahlam Ansari your Current Balance is Rs.107890.0/-
Welcome!!
Ahlam Ansari your Current Balance is Rs.96000.0/-

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
 */

No comments:

Post a Comment