user4594264
user4594264

Reputation:

Object and class analogy

Recently I faced an interview. They asked me "If this room is a class ,then what is object here? ". I could not answer that time. When I came back home , I thought "if room is a class , object is interview room/kitchen room(instance of room)."

Am I right ?

Upvotes: 0

Views: 811

Answers (2)

Sweeper
Sweeper

Reputation: 274835

I would say that you are right. But in your post you say "this room is a class. In this situation, I think it's more like an object. Because it means this particular room.

In my view, class is a concept and object is an example of that object. We have the concept "room" and this room is a room, so it's an object of the Room class.

Upvotes: 1

ekostadinov
ekostadinov

Reputation: 6950

My answer would be pretty much the same, taking in mind the Classes vs. Objects explanation in Code Complete, 2nd Edition

A key concept in object-oriented design is the differentiation between objects and classes. An object is any specific entity that exists in your program at run time. A class is the static thing you look at in the program listing. An object is the dynamic thing with specific values and attributes you see when you run the program. For example, you could declare a class Person that had attributes of name, age, gender, and so on. At run time you would have the objects nancy, hank, diane, tony, and so on—that is, specific instances of the class. If you’re familiar with database terms, it’s the same as the distinction between “schema” and “instance.” You could think of the class as the cookie cutter and the object as the cookie. This book uses the terms informally and generally refers to classes and objects more or less interchangeably.

And I would just add to

if room is a class , object is interview room/kitchen room(instance of room)

at runtime.

Upvotes: 1

Related Questions