DEV Community

Daniel Reis
Daniel Reis

Posted on

Object in Java(behavior and states)

Object Definition

Object mean a one part for software bundle. Object share two characteristics: They all have state and behavior!

State

State represents an object caracteristic, two examples is:

  • Dog has name, color, breed, hungry( #dogstates ) like states and this states has any "primitive information"(Behavior transform primitive information into "dynamic information", of course exists states not dynamic but object characteristic, "Name", "Color", "Breed" not possible to have a behavior in this example, this informations are final object in Java)

  • Bicycles has current gear, current pedal cadence, current speed( #bicyclesstates )

Behavior

Behavior are a comportament only or add state to dynamicity, "How?". See this in the next examples:

  • Dog states " #dogstates" and the dogs behavior(comportament only) wagging tail, fetching, barking, behavior with state: hungryDog(!boolHungryDog = "x" || boolHungryDog = "x")
  • Bicycles states " #bicyclesstates ", all bicycles behavior depends bicycles states, see bicycles behavior now: Gear, Cadence, Speed all mentioned behavior depends bicycles states

Code:

EXAMPLE 1:

public class Dog {  
    final String nameDog = "Teo";  
    final String colorDog = "Black and white";  
    final String breedDog = "Shi-Tzu";  
    public Boolean boolHungryDog = false;  
    public String hungryDog = " ";  

    public Dog() {  
        if (!boolHungryDog) {  
            hungryDog = "Teo is hungry, feed him! >:(";  
        } else {  
            hungryDog = "Teo is not hungry, HAPPY :)";  
        }  
    }  

        public static void main(String args[]) {  
            Dog myDog = new Dog();  
            System.out.println("My dog " + myDog.nameDog + " has " +
            myDog.colorDog + " color" + " my dog the breed " + myDog.breedDog +
            " and " + myDog.hungryDog);  
    }  
}
Enter fullscreen mode Exit fullscreen mode

EXAMPLE 2:

public class Bicycles {
    public int gear = 0;
    public int cadence = 0;
    public int speed = 0;

    void changeCadence(int newValue){
        cadence = newValue;
    }
    void changeGear(int newValue){
    gear = newValue;
    }
    void speedUp(int increment){
        speed = speed + increment;
    }
    void applyBrakes(int decrement){
    speed = speed - decrement;
    }
void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

class BicycleDemo {
    public static void main(String[] args) {

        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)