Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are classes, references and objects?

I've been programming java for 2 years now, and apparently I have encountered a problem where I couldn't understand and differentiate class, reference, and an object again (I do not get why I forget these concepts).

Lets get to down to the problem, which is that I am not sure if a class or reference are the same, though I have already an idea what is object.

Can someone differentiate in a nice and understandable and complete manner what are classes, references and object?

All I know is that the class is more like of a template for an object (blueprint to a house where the class is the blueprint and the house is an object).

like image 353
user962206 Avatar asked Feb 10 '12 07:02

user962206


People also ask

What is reference and object?

A reference is an entity which provides a way to access object of its type. An object is an entity which provides a way to access the members of it's class or type. Generally, You can't access an object without a reference to it.

What is a class reference?

A class reference is a special meta type that can be used to refer to classes (not instances of them) within a certain subtree of the class hierarchy. This allows to write code that can work polymorphically with classes – for example dynamically instantiate different subclasses or call virtual static methods.

What are classes and objects with example?

Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

What is object reference with example?

A variable whose type is a class contains a reference to an object of the class (i.e., the address of the memory location where the object is allocated). Example: String s; s = "xxx"; The first statement declares a variable s of type String.


2 Answers

If you like housing metaphors:

  • a class is like the blueprint for a house. Using this blueprint, you can build as many houses as you like.
  • each house you build (or instantiate, in OO lingo) is an object, also known as an instance.
  • each house also has an address, of course. If you want to tell someone where the house is, you give them a card with the address written on it. That card is the object's reference.
  • If you want to visit the house, you look at the address written on the card. This is called dereferencing.

You can copy that reference as much as you like, but there's just one house -- you're just copying the card that has the address on it, not the house itself.

In Java, you can not access objects directly, you can only use references. Java does not copy or assign objects to each other. But you can copy and assign references to variables so they refer to the same object. Java methods are always pass-by-value, but the value could be an object's reference. So, if I have:

Foo myFoo = new Foo();     // 1 callBar(myFoo);            // 2 myFoo.doSomething()        // 4  void callBar(Foo foo) {     foo = new Foo();       // 3 } 

Then let's see what's happening.

  1. Several things are happening in line 1. new Foo() tells the JVM to build a new house using the Foo blueprint. The JVM does so, and returns a reference to the house. You then copy this reference to myFoo. This is basically like asking a contractor to build you a house. He does, then tells you the house's address; you write this address down.
  2. In line 2, you give this address to another method, callBar. Let's jump to that method next.
  3. Here, we have a reference Foo foo. Java is pass-by-value, so the foo in callBar is a copy of the myFoo reference. Think of it like giving callBar its very own card with the house's address on it. What does callBar do with this card? It asks for a new house to be built, and then uses the card you gave it to write that new house's address. Note that callBar now can't get to the first house (the one we built in line 1), but that house is unchanged by the fact that a card that used to have its address on it, now has some other house's address on it.
  4. Back in the first method, we dereference myFoo to call a method on it (doSomething()). This is like looking at the card, going to the house whose address is on the card, and then doing something in that house. Note that our card with myFoo's address is unchanged by the callBar method -- remember, we gave callBar a copy of our reference.

The whole sequence would be something like:

  1. Ask JVM to build a house. It does, and gives us the address. We copy this address to a card named myFoo.
  2. We invoke callBar. Before we do, we copy the address written on myfoo to a new card, which we give to callBar. It calls that card foo.
  3. callBar asks the JVM for another house. It creates it, and returns the new house's address. callBar copies this address to the card we gave it.
  4. Back in the first method, we look at our original, unchanged card; go to the house whose address is on our card; and do something there.
like image 132
yshavit Avatar answered Oct 11 '22 20:10

yshavit


When you code, you build an

Instance (occurrence, copy)

of an

Object

of a said

Class

and keep a

reference

to it, so you can call its methods.

Also, some OOP basics: Classes, Object, Instance, and Reference.

like image 34
Marcelo Avatar answered Oct 11 '22 21:10

Marcelo