Goodmorning,
I have an idea, but I do not get it 100% yet..
Do I understand it right that:
A reference is just a reference to an object (like the box-example, it is just a note with a phone number that refers to the object neighbour?)
If this is true, why does the reference has a name, and not the object itself?
How do you then differentiate objects? because they all have the same name new NameofClass();
Are an object and an instance the same?
What is the difference between a reference, and object and an instance..
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Artem Divertitto Senior Android Developer at United Tech
27 November 2018, 15:36
In OO Programming, we often hear of terms like “Class”, “Object” and “Instance”; but what actually is a Class / Object / Instance?
In short, An object is a software bundle of related state and behavior. A class is a blueprint or prototype from which objects are created. An instance is a single and unique unit of a class.
Example, we have a blueprint (class) represents student (object) with fields like name, age, course (class member). And we have 2 students here, Foo and Bob. So, Foo and Bob is 2 different instances of the class (Student class) that represent object (Student people).
Let me go into details…
Object
Real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). Car have state (current speed, current gear) and state (applying brake, changing gear). Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields and exposes its behavior through methods.
Class
Class is a “template” / “blueprint” that is used to create objects. Basically, a class will consists of field, static field, method, static method and constructor. Field is used to hold the state of the class (eg: name of Student object). Method is used to represent the behavior of the class (eg: how a Student object going to stand-up). Constructor is used to create a new Instance of the Class.
Instance
An instance is a unique copy of a Class that representing an Object. When a new instance of a class is created, the JVM will allocate a room of memory for that class instance.
+4
hidden #10179195
27 November 2018, 15:07
I’m happy to help...
0
Sabine Meijran-Prins
27 November 2018, 14:49
Thank you Sephiroth,
You helped me a lot :)
+1
hidden #10179195
27 November 2018, 14:20
Hello...
References have a name because there can be more than one reference to the same object.
Ex: if the object is a house, references to this could be a photo, a drawing, a model, etc.
The objects are differentiated, among them, by the references of each one to the variable to which it has been assigned.
Ex: House house1 = new House ();
House house2 = new House ();
Here two "House" objects are created, references to them are assigned to two different variables, house1 and house2.
The objects are instances of a class:
Ex:
Building (superclass):
Floor1
Floor2
Floor3 (classes):
Floor1 Department1
Floor1 Department2
Floor1 Department3 (objects).
We could say that the object "Floor1 Department1" is an instance of the class "Floor1".
I hope it helps you ...
0