0

I have seen a lot of posts about objects, classes and instances and have become a little confused. I need clarification about the following example.

Say I create a game, it has three menus:

  • main menu,
  • level menu
  • option menu.

Now say I define a class called GameMenu since all these menus will have titles and instructions and buttons etc... Then I instantiate my menus using this class for example:

MainMenu = GameMenu();
LevelMenu = GameMenu();
OptionMenu = GameMenu();

And I pass the parameters for the titles and button labels and such inside the brackets - In the context of this example am I right in saying that my class is GameMenu, my objects are COLLECTIVELY MainMenu, LevelMenu and OptionMenu, and my instances are one specific object so MainMenu is an instance, OptionMenu is an instance and LevelMenu is an instance.

That is what I was taught, but that means objects and instances are the exact same things in which case the terms object and instance are synonyms for each other which is not what I have read?

Long story short, are object and instance terms synonyms?

Oleksandr.Bezhan
  • 2,028
  • 3
  • 22
  • 31

3 Answers3

2

An object is an instance of a class

AD.Net
  • 13,352
  • 2
  • 28
  • 47
0

Yes, you are right in saying so. Object and instance are synonyms. http://en.wikipedia.org/wiki/Instance_(computer_science) http://en.wikipedia.org/wiki/Object_(computer_science)

SAlexandru
  • 11
  • 2
0

Object and instance can be used almost synonymously in the context of OOP, however instance is a more general term.

For example: an object is an instance off a class, and also a process is an instance of a program.

In practice, it sounds odd (to me at least) to refer to an object as an instance unless we are talking about it in terms of the class of which it is an instance.

With the following code:

Foo a = new Foo();
function int bar(Foo f) { return 0; }

I would say:

`Foo` is a class.
`a` is an object.
`a` is an instance of Foo.
Function `bar` accepts instances of the class `Foo`
Alan
  • 3,307
  • 1
  • 19
  • 22