Trouble understanding Object State, Behavior, and Identity?
Asked Answered
P

5

27

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?

Pagandom answered 13/8, 2013 at 21:14 Comment(0)
E
54

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
Enjoy answered 13/8, 2013 at 21:20 Comment(6)
Thanks for the help, but I also need to know what they would consider a state, behavior, and identity. Those are the main things I didn't understand/was seeking help for.Pagandom
With the explanation for Identity, I think the identity is more with the unique object. Student id and email id would be the properties for the student.Seibold
properties that are unique in matter, and make this specific object unique in the domain. This is not the same as the hashcode of an object.Enjoy
This answered my question perfectly, thank you for helping me!Pagandom
if state is defined by the attributes then why people say static class does not have state? static class also got attributes. can u help me explaining it?Euphonious
sounds like you are talking from c# ? as java does not have "top" static classes, but you can do static nested classes, or using final classes, make all methods static etc... but anyway.. you should think of a static like your states are all the same, for all callers, there is only one copy... you don't need an instance of the class to use the "states". They are static, one copy, right.Enjoy
S
5

All objects have three essential features:

  • state
  • behavior
  • identity

An object's state is defined by the attributes of the object and by the values these have. In your case, name, age, gender

The term "behavior" refers to how objects interact with each other, and it is defined by the operations an object can perform. In your case, student object, can do some activity?

student.getResults();
  • Identity:

Even objects with the same properties and behavior have their own individual identity.

For Example,

2 blue station wagons that were built in the same year by the same manufacturer are still separate and unique cars.

The identity of an object is independent of its attributes or operations. So an object will retain its identity no matter what values its properties have.

Seibold answered 13/8, 2013 at 21:30 Comment(1)
I see. So Behavior would be as I explained, watching TV or talking to people. What about Identity though?Pagandom
S
5

As a correction to @MrSimpleMind's answer marked as best answer, and elaborating on @JNL's answer:

Identity is not what makes the object unique in terms of it's state (e.g. name = "Tim" or whatever). Identity however is that an object is unique in terms of it's location in memory.
If you what to read more about this, you can start by looking at this Wiki page: Identity in OOP

UPDATE
It's worth mentioning that it's not always in terms of location in memory. When saving an object to a database, it'll essentially be saved as a row, here an ID column is used.

Sooty answered 2/8, 2018 at 10:6 Comment(1)
There isn't any problem with @MrSimpleMind's answer as it's a conceptual description related to the way the question being asked. Even if you are technically right.Psychotomimetic
M
0

State:- state is values of objects variables

Behavior:- are methods of object

Identity:- It is name of object.

e.g:-

class car{

int speed = 100;

void accelerate(){ }

}

class_car obj_car;

here,

State:- speed

Behavior:- accelerate

Identity:- obj_car

Mascia answered 13/10, 2019 at 19:24 Comment(0)
A
0

In the programming implementation of an object,

its state is defined by its instance variables. Instance variables are private to the object. Unless explicitly made public or made available to other “friendly” classes, an object’s instance variables are inaccessible from outside the object.

An object’s behavior is defined by its methods. Methods manipulate the instance variables to create new state; an object’s methods can also create new objects. Java white paper page:34

Airlift answered 28/10, 2019 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.