Trouble understanding Object State, Behavior, and Identity?

TheNewGuy picture TheNewGuy · Aug 13, 2013 · Viewed 54.6k times · Source

I have been instructed by my professor to introduce myself on a page as if I were an object, and that I must address three things:
1) Object State, 2) Behavior, and 3) Identity.

However, I am still really confused as to how I would go about doing something like this. (I have read about the three attributes I must address, but I don't know how I would apply it to a person).
For example, I was told a dog would have States, such as name, color, and breed; as well as Behaviors, such as walking, barking, or wagging their tail.

So would I do something similar to:

Student me = new Student();
System.out.println(me.getName()); //a state?
System.out.println(me.getCurrentActivity()); //A behavior? (if it were to return watching TV or something) 
System.out.println(me.get....()); //???

Or am I getting the completely wrong idea here?

Answer

MrSimpleMind picture MrSimpleMind · Aug 13, 2013

Characteristics of objects are:

State: what the objects have, Student have a first name, last name, age, etc

Behavior: what the objects do, Student attend a course "Java for beginners"

Identity: what makes them unique, Student have Student-ID-number, or an email which is unique. (this is important when implementing the equals method, to determine if the objects are different or not)

Student john = new Student("John");
john.setCurrentActivity("Learning Java");
john.setAge(21);
john.setWeight(173);
john.setAddress(...);
john.setHobbies(...);

and you can figure out the getters.

public class Student {
    private String name;
    private int    age;
    //etc

    // construct a new student 
    public Student(String name) {
        this.name   = name;
    }

    public setAge(int age) {
        this.age   = age;
    }

    public int getAge() {
        return age;
    }
}

An illustration of a Car object, which I found that might help you some...

Car state:

  • Speed
  • RPM
  • Gear
  • Direction
  • Fuel level
  • Engine temperature

Behaviors:

  • Change Gear
  • Go faster/slower
  • Go in reverse
  • Stop
  • Shut-off

Identity:

  • VIN
  • License Plate