Object-Oriented Language

Reading

You must read

Java is an object-oriented language. What does it mean?

Real World Object

Look around you and you can see many examples of real-world objects ,example computer, desk, car, dog......
PC desk car dog
These are objects in the real world. These real-world objects share two characteristics: They all have state and behavior.
For example, dogs have state (name, color, breed ...... )
dog
Name : Bruin
Color : Brown
Dogs have behavior (sleeping, running, kissing???)






dog sleep
Sleeping
dog run
Running
dog play
Kissing





Cars have states (color, model, .....)


Red Car

Color :Red

Model : Toyota Camry





Cars have behavoirs (running, braking, backing up...)

car running
Running
Car Brake
Braking
car backing up
Backing up

Software objects are modeled after real-world objects in that they too have state and behavior . A software object maintains its state in one or more variables. A variable is an item of data named by an identifier . A software object implements its behavior with methods . A method is a function (subroutine) associated with an object.


Software objects are modeled after real-world objects in that they too have state and behavior .

A software object maintains its state in one or more variables.
A variable is an item of data named by an identifier .

A software object implements its behavior with methods .
A method is a function (subroutine) associated with an object.

Object

What is Class?

Definition: A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind.
(too abstract? don't worry. You don't have to understand it now.
You just need to know how to define a class, see below.)

Example: BankAccount

Question: What kind of properties(states) does a bank account have?
Example : saving, your name.
Question: What kind of things you can do to the back account?
Example : deposit money, withdraw money, get the saving, get the name.
statesaving, name
behaviordeposit, withdraw, getName, getSaving

How to use program to represent a bank account?

 1:public class BankAccount {
 2:    private int saving; // current amount of money you save in the bank account
 3:    private String name; // your name
 4:    
 5:    public BankAccount(String myName, int mySaving) {
 6:        name = myName;
 7:        saving = mySaving;
 8:    }
 9:    
10:    /** deposit money into the account */
11:    public void deposit(int depositAmount) {
12:        if(depositAmount >= 0) {  // depositAmount must be positive
13:            saving+= depositAmount;
14:        }
15:    }
16:    
17:    /** withdraw money from the account */
18:    public void withdraw(int withdrawAmount) {
19:        /* If the withdrawAmount is negative or
20:           there is not enough saving for withdraw
21:           do nothing */
22:        if(withdrawAmount >= 0 && withdrawAmount <= saving) { 
23:            saving-= withdrawAmount;
24:        }
25:    }
26:    
27:    /** return saving */
28:    public int getSaving() {
29:        return saving;
30:    }
31:    
32:    /** return name */
33:    public String getName() {
34:        return name;
35:    }
36:}      


Explanation

How to define a class?

public class MyClassName {
   // your codes
}
You MUST name the file MyClassName.java

e.g in BankAccount.java we have
public class BankAccount {
     // some codes
      // ......
}
or in HelloWorld.java
public class HelloWorld {
    .......
}

Variables

  1:public class BankAccount {
  2:      private int saving ;  // current amount of money you save in the bank account
  3:      private String name ; // your name
          ..........................
36: }


Explanations
saving and name are variables (state).
saving is an integer.

Syntax:

primitiveType   variableName;
className   variableName;


Method

  1: public class BankAccount {
       .............
11:     public void deposit(int depositAmount) {
                 ......
15:     }
           ..............
18:     public void withdraw(int withdrawAmount) {
                 .......
25:     }
            .....
28:    public int getSaving() {
29:           return saving;
30:    }
         ..........
33:    public String getName() {
34:          return name;
35:    }           

Explanation:
void deposit(int depositAmount) ,void withdraw(int withdrawAmount)
Take an integer as arguemnt, and return nothing.

int getSaving(),  (or String getName()):
No augument and return int, (or a String).

Syntax:
returnType methodName(type1 name1, type2 name2 , ......)

public and private modifer

  1: public class BankAccount {
  2:      private int saving;
  3:      private String name;
11:      public void deposit(int depositAmount)
18:      public void withdraw(int withdrawAmount)
28:      public int getSaving()
33:      public String getName()
private:
  1. A private member is accessible only to the class in which it is defined.
  2. This includes variables that contain information that if accessed by an outsider could put the object in an inconsistent state, or methods that, if invoked by an outsider, could jeopardize the state of the object or the program in which it's running.
  3. Private members are like secrets you never tell anybody.
e.g  for BankAccount.java 
  1. You don't want other people to change the name of the account.
  2. You don't want other people to change the saving amount other than using withdraw and deposit method.
public :
  1. Any class has access to a class public members.
  2. Declare public members only if such access cannot produce undesirable results if an outsider uses them.
  3. There are no personal or family secrets here; this is for stuff you don't mind anybody else knowing.
e.g  for BankAccount.java 
methods deposit, withdraw, getName, getSaving allow user to interact with bank accounts.