Mr. Demetrius Michael
Mr. Demetrius Michael

Reputation: 2406

OOP - If a class creates an instance of a class, does the instance become an object as well?

Not to sound like a koan, but just wondering if there are definite rules about classes and objects. I used to think classes as blueprints, and objects as the creation from them. But if a combination of blueprints creates another blueprint, does the latter blueprint become an object as well?

Upvotes: 0

Views: 75

Answers (2)

davioooh
davioooh

Reputation: 24706

Your question seems a bit philosophical... :) "object" and "instance" are quite synonymous in OOP.

If I understood your question correctly, your doubt is: "an object is still an object also if created by another class that is not the same that define its type?"

The answer is "yes", an instance is an object created following the "model" defined by its class, but for many reasons you could instantiate a class in an indirect way, for example a static method (factory method of a factory class, for example) and not directly using new statement.

If you want to see some come, an easy example in Java could be:

public class MyClass {
    public MyClass(){}
}

public class MyClassFactory{

    public getInstance(){
        return new MyClass();
    }
}

In this case the instance is not returned directly by MyClass, but from its factory class. however it's an object as well...

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882626

In just about every OO environment I know, an instance is the same as an object.

It doesn't matter whether the object/instance is created by the client (such as with new) or by the class (such as with singletons or factories).

If you're talking about blueprints in the context of classes, then creating blueprints from blueprints is inheritance, not instantiation.

Upvotes: 0

Related Questions