Passing primitive and reference types method parameters

-Parameters are pass-as-copy for primitive and reference data type
-Object will be passed as copy of reference  
-While passing primitive type as a method parameter, java passes copy of the primitive type to the calling method, so that the original value will be kept different from the copy, which results into two separate variables with separate memory locations.
Examples: int, long, double as a method parameters

-While passing reference type as a method parameter, java passes copy of reference to the calling method (copy of reference but not copy of Object). So original reference variable and its copy are pointing to the same Object memory location. which results into two reference variables pointing to the same Object memory location. With this you can modify Object states using either of the references (original or copy).
Fig. 1: Memory map for primitive and reference data types
-------------------------------------------------------------------------------------------------------------------------
Example: To demonstrate how java passes parameters for primitive and reference data types.
public class TestParamPass {
  public static void main(String[] args) {
  int age = 20; //primitive data type
  Employee emp = new Employee(); //reference data type
  System.out.println("Original age: " + age);
  System.out.println("Original salary: " + emp.getSalary());
  processAge(age, emp);
  System.out.println("Updated age: " + age);
  System.out.println("Updated Salary: " + emp.getSalary());
 }

  //Method to process passed parameters

  private static void processAge(int ageParam, Employee empParam) {
   empParam.setSalary(2000);
   ageParam++;
  }
}
Output:
Original age: 20
Original salary: 0.0
Updated age: 20
Updated Salary: 2000.0
------------------------------------------------------------------------
public class Employee {
     double salary;
     public double getSalary() {
          return salary;
     }
     public void setSalary(double salary) {
          this.salary = salary;
     }
}

Static Methods and Variables

-Can be accessed at class level
-Does not require Class instance (aka Object) 
-Static variable keeps only one value during entire program execution,
 unlike Instance variable, which maintains separate values (state) per instance (Object) 
======================================================
Example: To demonstrate Static and Instance variables and methods

public class AccountHandler {
     public static double MAX_DEPOSIT_LIMIT = 1000000;         
     //Instance method to deposit specified amount to specified customer Id
     public void doDeposit(String customerId, double amount) {
          if (amount > MAX_DEPOSIT_LIMIT) {
               System.out.println("Can not process, exceeding limits, Amount: " + amount);
               return; //return to caller
          }
          System.out.println("Depositing amount " + amount + " for Customer Id : " + customerId);
     }
     //Static method to show customer's ß for the specified customer Id
     public static void showPortfolio(String customerId) {
          System.out.println("Showing Portfolio for Customer Id : " + customerId);
     }
}
---------------------------------------------------------------------------
public class TestStatic {
    public static void main(String[] args) {
          //check account deposit limits by accessing static MAX_DEPOSIT_LIMIT
          System.out.println("Allowable deposit limit : " + AccountHandler.MAX_DEPOSIT_LIMIT);
          AccountHandler handler = new AccountHandler();
          //calling instance method by using dot operator on instance of AccountHandler class
          handler.doDeposit("a007", 10000);
          //calling static method by using dot operator on AccountHandler class
          AccountHandler.showPortfolio("a007");
     }
}

Instance variables, Local variables and Method Parameters


Instance variables:
-are mainly use to store objects's state 
-visibility can be controlled by using appropriate scope (public, private, protected,default)
-lifetime is limited to containing object's lifetime

Local variables and Method parameters:
-are local to specific method 
-visibility can not be controlled its only visible inside the defined method
-lifetime is temporary and only limited to method scope
======================================================
Example: To demonstrate different types of variables
public class Customer {   
     private String lastName; //instance variable
     private String firstName; //instance variable
    
     public String getLastName() {
          return lastName;
     }
     public void setLastName(String lName) { //parameter 
          lastName = lName;
     }
     public String getFirstName() {
          return firstName;
     }
     public void setFirstName(String fName) {
          firstName = fName;
     }
     //Returns full name by concatenating Last and First name
     public String getFullName() {
          String fullName; //local variable
          fullName = getLastName() + " " + getFirstName();
          return fullName;
     }
}

Polymorphism

- Sub-class can override superclass methods for any specific behavior implementation
- JVM dynamically calls appropriate method based on the inheritance hierarchy
- For the same method call the objects behave differently based on implementation
 
- Promotes reuse and design can be extended by adding more sub-classes without 
changing interface definition. Such as getLanguage() method in below example which takes Country as a parameter, so that it can be used for any Country type, such as Japan, America and Korea including any future new Countries. 
======================================================
Example: Polymorphism using method overriding

Description: Code to demonstrate polymorphic behavior in Java using inheritance and method overriding.


Country is superclass of all specific country sub-classes. The method getLanguage() can be overridden by sub-classes of Country class as shown below, where Japan and America are overriding default implementation of getLanguage() method from Country but Korea class is not overriding it. So when caller program (TestPoly) calls getLanguage() on the Country type the JVM will dynamically call appropriate 
getLanguage() method from sub-class, if method is not overridden in sub-class then JVM will call super class's method i.e. Method from Country in case of Korea.


Design: Class Diagram






Source: 

public class TestPoly {
    public static void main(String[] args) {
         Country[] countries = new Country[3];
         countries[0] = new Japan();
         countries[1] = new America();
         countries[2] = new Korea();    
         for (Country country : countries) {
              getCountryInfo(country);
         }
    }
    //method to print country specific information
    public static void getCountryInfo(Country country) {
         System.out.println(country.getLanguage());
    }
}
Output
Japan's National Language is Japanese
America's National Language is English
UN-KNOWN
--------------------------------------------------------
public class Country {   
     //default implementation
     protected String getLanguage() {
          return "UN-KNOWN";
     }
}
--------------------------------------------------------
public class Japan extends Country {   
     @Override
     public String getLanguage() {
          return "Japan's National Language is Japanese";
     }
}
--------------------------------------------------------
public class America extends Country {
     @Override
     public String getLanguage() {
          return "America's National Language is English";
      }
}
--------------------------------------------------------
public class Korea extends Country {


}

Approaching System Development (102L2)

·      Using Object Oriented Methodology for complete Software Application Development 

Start below activities in Inception Phase,

1.     Write brief “Vision” draft about the project
2.     Identify users’ goals and supporting use cases
3.     Write use cases and Supplementary Specification document
a.     Use cases (Ivan Jacobson, 1986)
                                                                 i.     Are stories using system functionality to meet desired system goals
                                                               ii.     Define Use-case model at requirements stage
                                                              iii.     It defines system functionality (what system will do) and environment
                                                              iv.     It includes interactions between Actors and System for realizing related scenarios such as success or alternate scenarios (failure).
                                                               v.     Scenarios yields observable results for particular actor

b.     Supplementary Specification includes,
                                                                 i.     Quality properties (Non Functional Requirements)
                                                               ii.     constraints
1.     Hardware (Linux due to company roadmap)
2.     Software (only from registered vendors)
3.     Development tools (eclipse)
                                                              iii.     Internationalization and Localization
                                                              iv.     Licensing and compliance
                                                               v.     Business rules
                                                              vi.     Business Contingency plan
                                                            vii.     Users, Installation manual and Help
                                                           viii.     Standard references (development, test, release)

4.     Refine the Vision during next iterations and phases
5.     Create Software Development Plan

Keep refining all above activities in the further phases

Start below activities in Elaboration Phase,

1.     Create Domain Model using Use case and Vision
2.     Write software architecture document
3.     Create Implementation Model
4.     Create Test plan

·      Online Banking System (ATM like system)
o   Requirements
Automate existing manual banking functions to serve Prospects and Customers efficiently and cost effectively. The scope is limited to below functions under Phase-I of this project.

§  Scope :
·      Customer registration system
·      Account transactions includes : deposit, withdrawal, transfer
·      Security management includes : User Authentication and maintenance
·      Some features will be available using web or mobile clients
o   Portfolio Analysis
o   Money Transfer
·      Transaction audit facility for Auditors and Customers

§  Use-case Model




§  Use-case Goals

Use case
Description
Manage Users
User data maintenance and security information will be managed by System Administrator
Authenticate Users
Authenticate Customer login information using existing security service
Money Deposit
Customer does money deposit which finally updates core banking system
Money Withdrawal
Customer does money withdrawal which finally updates core banking system
Money Transfer
Customer does money transfer to other bank accounts  which finally updates core banking system
Transaction Analysis
Customer and Auditor can view account transactions
New Account Registration
Prospect submit form to open new account with the bank which will be authorized or rejected by bank manager


o   Domain Model



o   Build Relationship between Domain Objects

§  Class Diagram



o   Add responsibilities
§  Add methods to each identified object
o   Add System Specific Objects
§  Such as TransactionManager above
o   Re-define objects relationship if necessary
o   Use MVC and layer components
o   Define non-functional requirements (Quality Properties)
§  HA (High Availability) and Performance requirements
§  Security Model
§  Internationalization and Localization
o   Do implementation using Java along with Unit test cases (JUnit).
o   Build GUI presentation layer.

{*Properly use Abstraction, Inheritance, Encapsulation and Polymorphism principles}


Object and Class

1) Object and Class
Class is template to create objects and represents classification of objects such as Employee, which is a class for the objects such as “Tom”, “Joe”, “Hanako”

2) OO-Analysis
            - Break the requirements into smaller problems (decompose the requirements)
            - Try to understand “WHAT” is require instead of “HOW” to implement
- Create conceptual domain model (only consider domain specific objects
   and  not system specific)

Such as in case of  our “Online Banking System” (OBS) Project following is a conceptual design (domain model)

3) OO-Design
o   For each identified object find out how they will cooperate and collaborate with each other to understand the relationship between them.

o   Then assign responsibility by adding properties and methods (behavior) to each object

o   Add helper objects (system specific and not business specific)
Such as TransactionManager

4) OO-Implementation:
o   Implement the design using specific OO Language, such as Java
-       Create new project in eclipse : OnlineBankingSystem
o   Create class : Customer

public class Customer {
       private String lastName;
       private String firstName;

       public String getLastName() {
              return lastName;
       }
       public void setLastName(String lastName) {
              this.lastName = lastName;
       }
       public String getFirstName() {
              return firstName;
       }
       public void setFirstName(String firstName) {
              this.firstName = firstName;
       }
}