Sunday 21 February 2016

Java: Inheritance Relationship with Example.

Association Vs Inheritance

Association: Association in Java establish relationship between two classes through their Objects. This relationship can be four type.

  • One to One
  • One to Many
  • Many to One
  • Many  to Many
Inheritance: Inheritance is a process where one class gain the properties (public field & method) of another class. The class which inherits the properties of other class is known as subclass(child/derived class) and the class whose properties will be inherited is known as super-class(parent/base class).

Difference: They both have code reuse ability but inheritance has the ownership of base class.

Keyword: extends

Case Study:

Suppose, In a Bank, there are two types of accounts.
  • Savings Account
  • Current Account
Both accounts have the following fields
  • Account Number
  • Account Name
  • Balance
and methods
  • Deposit
  • Withdraw
Savings account has interest rate and 
current account has service charge field.

Lets see the class diagram:
Class Diagram
Here, BankAccount is parent class and SavingsAccount & CurrentAccount are child class.

Savings account is not allowed to null the account balance and current account is allowed to take loan from bank.
So we have to overwrite the withdraw method for logic implementation.

Lets see the following code:

BankAccount Class:

// ADLabs


public class BankAccount {

 private String accountNO;
 private String accountName;
 private double balance;

 public BankAccount(String accountNO, String accountName) {
  this.accountNO = accountNO;
  this.accountName = accountName;

 }

 public BankAccount() {
  balance = 0;
 }

 public void deposit(double money) {
  balance += money;
  System.out.println("Money Successfully Deposited!");
 }

 public void withdraw(double money) {
  balance -= money;
  System.out.println("Balance :" + balance);
 }

 public double getBalance() {
  return balance;
 }

 public void setBalance(double balance) {
  this.balance = balance;
 }

}

SavingsAccount Class :
// ADLabs


public class SavingsAccount extends BankAccount {

 private float interestRate;

 public SavingsAccount(String accountNO, String accountName, float interestRate) {
  super(accountNO, accountName);
  this.interestRate = interestRate;
 }

 public void monthEndBalance() {
  float money = (float) (getBalance() + (getBalance() * (interestRate / 12)));
  System.out.println("Month End Balance will be :" + money);
 }

 public void withdraw(double money) {
  // Overwrite the super method.
  if (getBalance() - money >= 500) {
   super.withdraw(money);
  } else
   System.out.println("Insufficient Balance!");
 }
}


CurrentAccount Class:

// ADLabs


public class CurrentAccount extends BankAccount {
 private float serviceCharge;

 public CurrentAccount(String accountNO, String accountName, float serviceCharge) {
  super(accountNO, accountName);
  this.serviceCharge = serviceCharge;
 }

 public void monthEndBalance() {

  float money = (float) (getBalance() - (getBalance() * (serviceCharge / 12)));
  System.out.println("Month End Balance will be :" + money);
 }

 public void withdraw(double money) {
  // Overwrite the super method.
  double check = getBalance() - money;
  if (check < 0) {
   super.withdraw(money);
   System.out.println("You Loan from Bank :" + check);
  }
 }
}


main Class:
// ADLabs


public class StartPoint {

 public static void main(String[] args) {

  SavingsAccount account1 = new SavingsAccount("SA-1012", "Kopa Samsu", 5);
  CurrentAccount account2 = new CurrentAccount("CA-1023", "Musa Bin", 7);

  account1.deposit(5000);
  account2.deposit(5000);
  account1.monthEndBalance();
  account2.monthEndBalance();
  account1.withdraw(1000);
  account2.withdraw(5500);
 }

}


Console:

Money Successfully Deposited!
Money Successfully Deposited!
Month End Balance will be :7083.3335
Month End Balance will be :2083.3335
Balance :4000.0
Balance :-500.0
You Loan from Bank :-500.0


( ͡° ͜ʖ ͡°)
Happy Coding :)
-@D