OOPs Interview Mastery›class vs object
Card 1 of 5

What a class is

A class is a blueprint: a type you declare in source code that describes a kind of thing. Picture the shopping cart in a grocery app. The class Cart says what a cart in the app is, but writing the declaration does not give any shopper a real cart.

class Cart {}

This one line declares the Cart class. It is a plan that lives in your code, like a cookie cutter before any dough is pressed into it.

1 / 5

Class vs object

Lesson 1 of 84 in OOPs Interview Mastery. Free to read, no account needed.

What a class is

A class is a blueprint: a type you declare in source code that describes a kind of thing. Picture the shopping cart in a grocery app. The class Cart says what a cart in the app is, but writing the declaration does not give any shopper a real cart.

class Cart {}

This one line declares the Cart class. It is a plan that lives in your code, like a cookie cutter before any dough is pressed into it.

Creating an object with new

An object is an instance of a class: a real thing made from the blueprint while the program runs. You create one by writing the new keyword followed by the class name and brackets. Java starts a program in a method called main. You will meet main properly later, so for now just read the new line as the thing that creates the object.

class Cart {}
class Demo {
  public static void main(String[] args) { new Cart(); }
}

The line class Cart {} is the blueprint. The line new Cart(); inside main is the one that creates an object: a real cart built from the Cart blueprint.

One class, many objects

One class can produce many objects. Each new makes a separate object from the same blueprint, so three shoppers can each get their own cart, and none of the carts is the same thing as another.

class Cart {}
class Demo {
  public static void main(String[] args) { new Cart(); new Cart(); new Cart(); }
}

The three new Cart(); statements create three Cart objects. They all come from the one Cart class, like a cookie cutter that makes three cookies, and each cookie is its own separate cookie.

Declaring is not creating

Writing a class does not create an object. The declaration only describes the kind of thing. The number of objects depends only on how many times new runs. In each row below, the program is class Cart {} plus a class Demo whose main method body contains the statements shown.

Statements in mainCart objects created
none0
new Cart();1
new Cart(); new Cart();2

The class is never an object by itself. Only the new statements create objects.

Class decides object kind

An object is an instance of its class, and its class decides what kind of object it is. Here we declare two classes, Cart and Order, and then create objects from each. Both declarations sit at the top of the file, and the new statements sit in main.

class Cart {}
class Order {}
class Demo {
  public static void main(String[] args) { new Cart(); new Cart(); new Order(); }
}

The two new Cart(); statements create two shopping carts, separate from each other. The new Order(); statement creates an order. A Cart object is always a cart and never an order, because its class decides its kind.

Practice questions

  1. 1.

    A file declares class Cart {} and its main method contains three statements, each written new Cart();. What does each of those statements build its object from?

    • A.

      One shared Cart object that all three statements reuse

    • B.

      The Cart class, reusing its first object for later new calls

    • C.

      The Cart class blueprint, building a new separate object each time

    • D.

      A Cart object already created when the class is declared

    Show answer
    Answer: C.

    The Cart class blueprint, building a new separate object each time

    The class Cart is a blueprint; each executed new Cart(); builds a fresh, separate object from it. Option 1 is the misconception of one shared object reused by every new. Option 4 wrongly treats declaration as creation, since objects exist only once new runs. Option 2 wrongly gives the class a reuse behaviour: the class never hands out its earlier object.

  2. 2.

    Main's only statement is new Order();. Which change makes it create a Cart object?

    • A.

      Rename the class Order {} declaration to Cart

    • B.

      Move the class Cart {} declaration below main

    • C.

      Delete the class Order {} declaration

    • D.

      Replace it with new Cart();

    Show answer
    Answer: D.

    Replace it with new Cart();

    The class named after new sets the kind of object created, so replacing new Order() with new Cart() creates a Cart. Renaming the Order declaration to Cart leaves new Order() pointing at a class that no longer exists, so the code fails to compile. Moving the Cart declaration does not change which class new uses, so the object would still be an Order. Deleting the Order declaration also leaves new Order() pointing at a missing class, so the code fails to compile and creates nothing.

  3. 3.

    Classes Cart and Order are declared as empty classes. How many objects does this main body create when run?

    new Cart();
    new Order();
    new Cart();
    new Cart();
    • A.

      Four, one object for each new expression executed

    • B.

      One, only the class holding main creates objects

    • C.

      Three, identical new Cart() lines share one object

    • D.

      Two, one object for each class declared

    Show answer
    Answer: A.

    Four, one object for each new expression executed

    Each executed new expression creates its own separate object, and the four new expressions here run once each, so four objects are created. The option saying two counts one object per declared class, which ignores how many times new runs. The option saying three treats identical new Cart() lines as sharing one object, which is wrong because each new makes a separate object. The option saying one assumes only the class holding main creates objects, but new expressions inside main create objects regardless of which class they sit in.

  4. 4.

    Given these declarations and this main method, how many objects does the program create when it runs?

    class Cart {}
    class Order {}
    class Demo {
    public static void main(String[] args) {
    new Cart();
    }}
    • A.

      Three

    • B.

      Zero

    • C.

      One

    • D.

      Two

    Show answer
    Answer: C.

    One

    Only the new Cart(); expression runs, so exactly one object is created. The class Cart and class Order declarations create no objects. 'Two' assumes each declared class creates an object. 'Zero' assumes that declarations alone create nothing, but the new expression does create one. 'Three' counts every class plus the Demo class.

  5. 5.

    Assume class Cart {} and class Order {} are declared. Inside main, these three statements run in order, top to bottom:

    new Order();
    new Cart();
    new Cart();
    

    Which statement is the first to create a Cart object?

    • A.

      The class Cart {} declaration, before main runs.

    • B.

      The first line, new Order();

    • C.

      The third line, the second new Cart();

    • D.

      The second line, the first new Cart();

    Show answer
    Answer: D.

    The second line, the first new Cart();

    A Cart object exists only once its own new Cart(); has executed at run time. The new Order(); line creates an Order, not a Cart, so it cannot be the first Cart creator. The later new Cart(); line is the third line, which runs after the second line has already created a Cart object. The class Cart {} declaration creates no object at all, because declaring a class only describes it.

  6. 6.

    In Java, when is a Cart object created from the declaration class Cart {}?

    • A.

      Only when the program runs and executes new Cart().

    • B.

      When the Cart class is declared in the source code.

    • C.

      As soon as javac compiles the Cart class declaration.

    • D.

      When the source file containing class Cart is saved.

    Show answer
    Answer: A.

    Only when the program runs and executes new Cart().

    Objects are created at run time, and only by executing a new expression. Compiling with javac produces bytecode but creates no Cart object. Declaring the class in source code describes a blueprint and creates nothing. Saving the file is an editor action that has no effect on objects at all.

  7. 7.

    Which of these is a declaration rather than an object creation?

    • A.

      new Cart();

    • B.

      class Cart {}

    • C.

      new Order();

    • D.

      new Cart(); new Cart();

    Show answer
    Answer: B.

    class Cart {}

    class Cart {} is a declaration: it describes the Cart blueprint and creates no object. Each of the other options runs a new expression, and each new creates an object, so none of them is a declaration.

  8. 8.

    Given class Cart {} is declared, and main runs these two lines, what is true of the two Cart objects?

    new Cart();
    new Cart();
    • A.

      Neither exists; only the class declaration is present.

    • B.

      Two separate objects, both built from the Cart class.

    • C.

      The second new reuses the first existing Cart object.

    • D.

      One shared object, since both come from Cart.

    Show answer
    Answer: B.

    Two separate objects, both built from the Cart class.

    Each executed new expression creates its own object, so the two new Cart(); lines create two separate Cart objects from the same blueprint. The option saying one shared object is wrong because sharing a class does not share objects; each new makes a new one. The option saying the second new reuses the first is wrong because new always creates a fresh instance and never reuses one. The option saying neither exists is wrong because executing new at run time does create objects.

  9. 9.

    Class Demo's main method contains new Cart();, but no Cart class is declared anywhere in the file. What happens when javac compiles the file?

    • A.

      It compiles, and creates a generic object of no particular kind.

    • B.

      It compiles, but creates no object, since Cart has no class body.

    • C.

      Compilation fails, because new needs a declared Cart class to build from.

    • D.

      It compiles, and the new line creates a Cart object anyway.

    Show answer
    Answer: C.

    Compilation fails, because new needs a declared Cart class to build from.

    The new keyword builds an instance from a named class, and a class must be declared to serve as that blueprint. With no Cart declaration, javac reports an unknown class error, so option stating compilation fails is correct. The option saying it compiles and creates a Cart object is wrong because no Cart blueprint exists. The option claiming no object because of an empty body confuses an empty class, which is still a valid blueprint, with a missing declaration. The option claiming a generic object is wrong because every object's kind comes from its class.

  10. 10.

    A program runs the statement new Cart(); and never refers to its result again. What happens when the statement runs?

    • A.

      A Cart object is created, even though the program never touches it.

    • B.

      No object is created, because the unused result is discarded.

    • C.

      The compiler rejects the statement as having no effect.

    • D.

      A Cart.class file is created instead of an object.

    Show answer
    Answer: A.

    A Cart object is created, even though the program never touches it.

    The new expression runs at run time and creates a Cart object whether or not its result is used later, so option 1 is correct. Option 2 wrongly ties object creation to whether the result is used. Option 3 is wrong because a bare new statement is valid Java and compiles. Option 4 confuses run-time object creation with compilation output, since a .class file is produced by javac, not by running new.

Take these as a timed quiz

Lessons in Objects and Classes Basics

  1. Class vs object
  2. Fields and methods
  3. Constructor invocation
  4. Reference variable vs object
  5. Private fields with getters
  6. Public vs private vs protected
  7. Package-private default access
  8. == vs equals
  9. Object identity vs equality
  10. Equals and hashcode contract

84 lessons across 9 topics. See the full course