This content originally appeared on DEV Community and was authored by Jeeva Aj
Bank project:
New keyword allocates a memory space bank employee1 = new bank ();
Bank => class
Syntax for object creation: class name ref.name = new class name ();
() => constructor
Object => employee
Employee min balance, details, sat & sun leave details = called states
Employee debit card, credit card, loan sanctions = called behaviour.
So, object is known as combination of states & behaviours.
Object is a memory reference of a class.
Object is a physical entity.
Object represents the class.
Object is an instance of a class. ( it occupies a space in the logic entity that is bank).
Example 1 Robot robot1
Robot.getGrocery (200); 1.getGrocery (packet owner-cash){}
( 200 ) => argument/parameter 2.getGrocery(bag grocery amount){}
getGrocery => method or actions
Cash => packet
Change => bag
200 => int
200.5 => float
Packet, bag, int, float are called containers because they occupy a storage.
Example 2 Robot getJuice (100);
Bottle.getJuice ( int amount){
1.amount
2.juice
Return juice
}
Example 3 getFood (200);
Bag.getFood ( int amount) {
1.amount
2.food
Return food
}
Action => getFood
200rs (input) => int
Here, robot deposited the amount but did not bring anything back to home. So it will call it as void. No return => void.
Our project code in eclipse for understanding:
package bank.loan;
public class Bank {
public static void main(String[] args) {
Bank employee1 = new Bank(); //allocates a memory space
employee1.deposit(100,1111); //method calling statement
//method name: deposit
//arguments: 2
//arguments(100,1111)--> parameters
Storing container => bag
So, food is an action and the corresponding storage or datatype is bag. Write in a meaningful way that matches the corresponsing actions to container.
Example 4 robot.withdraw(1234);
Int Withdraw(int pin) {
1.
2.
Return amount
}
Here, returning amount will be an integer so it is called as int.
Example 5 robot.deposit (100);
Robot. Deposit (100, 12345);
void.deposit (int amnt, int accNo){
1.
-
}
int money = employee1.withdraw(100,1111);
System.out.println(money);}
private int withdraw(int cash, int accno) {
return 100;}
private void deposit(int amount, int accNo) //method signature
{
//method body or method definition
System.out.println ( “Deposit” + 100);
System.out.println (“in acc no:” + accNo);}
}
This content originally appeared on DEV Community and was authored by Jeeva Aj