In Java, what’s the difference between an object and a class?


This kind of question is a test of your knowledge of the terminology associated with object oriented programming. You should note that this question could just as well be asked in the context of a C++ programmer interview, or any programming position that requires object oriented programming for that matter.

The terms ‘class’ and ‘object’ are definitely related to one another, but each term holds its own distinct meaning. Let’s start out by explaining what the term “class” means in the context of OOP. The term ‘class’ refers to the actual written piece of code which is used to define the behavior of any given class. So, a class is a static piece of code that consists of attributes which don’t change during the execution of a program – like the method definitions within a class.

An object is an instance of a class

The term ‘object’, however, refers to an actual instance of a class. Every object must belong to a class. Objects are created and eventually destroyed – so they only live in the program for a limited time. While objects are ‘living’ their properties may also be changed signficantly.

An example will help clarify what we’ve said. Suppose we have a class called ‘Animal’. All Animals have bodies and brains – and these could be the attributes of our fictional Animal class. We can also add some methods that would be common to all Animals – like “movement”, because all animals can move (maybe you can think of a better example for methods, but hopefully you get the point). So, the idea you really want to enforce in your own mind is that this very general ‘template’ of an Animal does not change – it’s simply just some lines of code that define the Animal class.

An instance of the Animal class would be a specific animal – like a lion, a cat, or a zebra. These instances of the Animal class would be called objects. Whereas the Animal class is a general concept, the instances of that class – the lions, cats, etc – take that general concept and create a real instance of it.

That is why programmers define constructors for their classes – so that when someone wants to create an object of the class, he/she can just pass in the actual properties that he/she wants the object to have – like what kind of animal it is, the name, the weight, etc. So, you can think of a constructor as something that brings the class to life – which is why it is called a constructor, because it constructs a specific instance of a class.

Objects have a lifespan but classes do not

And, as our Animal example clearly shows, every object has a lifespan associated with it – a cat or zebra can not live forever. And, the properties of those objects can change as well while they ‘live'; if we have a ‘size’ variable defined in the class that would of course change as the cat object grows bigger.

Object versus class summary

So, we can say that whereas a class is a general concept (like an Animal), an object is a very specific embodiment of that class, with a limited lifespan (like a lion, cat, or a zebra). Another way of thinking about the difference between a class and an object is that a class provides a template for something more specific that the programmer has to define, which he/she will do when creating an object of that class.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon
  • Eddy Ekofo

    Thank you for that! God bless

  • notes4geeks
  • Josh

    Great ! I have a silly doubt to ask –

    If object Lion belongs to class Animal, will the specific characteristics of Lion (i.e. animal with fur or an animal known as the king of jungle) be defined for the object (Lion) or in the generic class template (Animal)

    b) Is it right to call the attributes that we assign to the class or object as methods (instructions) ?

    Thank you.

  • Jain

    Thanks. Easy to understand

  • bufflowbill

    thank you. taking a class now and that helped

  • faizan

    easy to understand the concept of class and object thanks

  • hriday dubey

    Class is a template for creating objects which defines its state and behavior. A class contains field and method to define the state and behavior of its object.
    for more visit here

  • Abdelouahab Abdenour Aliane

    thank you ^_^
    note that in python, object are classes!

  • Sudha

    I have a doubt.. Will u please help me for clarifying? Is class first or object? what executes first?

    • Gustav Streicher

      You will probably first create the class, which exists in your code, and then write the instantiation instruction code of the object which will also exist in your code. When your program is executed the instantiation instructions with the properties of the object that you have defined will be sent to the class and then an object with those properties will be created in your computers memory and removed at the point where you ‘coded’ it to be removed thereby completing its lifetime.

  • Michael

    But some objects can also be classes some times, i.e a lion is an object of the animal class, but also a lion class. isn’t so?

  • Sunil Rawat

    • p


  • Shubham

    Beautifully explained, thanks.

  • vamsi

    good one

  • Robert

    Surely Cat, Lion, Zebra etc. are classes, with a common ancestor of Animal related through the OO concept of inheritance.
    Specific instances (objects) would be individual animals — e.g. that cat on the chair, who is called Fluffy, is four years old, weighs 10 kg and had fish for dinner.

    • Matt

      You’re right. Cat, Lion and Zebra are sub-classes of the Animal class. Only when the class is initialized does it become an object (An actual instance of a class). I would describe a class as a blueprint of an object.

      • Matt

        Woops I mean instantiated not initialized.

    • Yawn

      ‘Cat’ is a class but ‘a cat’ is an object.

  • anonymous

    Good explanation and easy to understand..

  • incognito

    Nicely explained with simple example, just the way I like it 😉