Nearby lessons
30 of 125Java - Objects and Classes
- What class and object mean, with real-life examples
- The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Constructors, this, super, static and final
- Method overloading and method overriding (with a comparison table)
- Abstract classes and interfaces (including the modern changes)
- Association, Aggregation and Composition
Objects and Classes is a core concept of the Java language. This lesson explains Class and Object, The Anatomy of an Object Creation and Class vs Object — Question and Answer with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Class and Object
What is a Class?
A class is a blueprint or template. It defines what properties (data) and behaviours (methods) an object will have. A class alone does not occupy any space in memory — it is just a design.
What is an Object?
An object is the real thing created from the blueprint. It is an instance of the class. Each object has its own copy of the data, and it lives in memory (in the heap area).
Real-life example: a Student admission form is a class (blueprint) — it tells you which fields a student has. The actual student 'Rahul' with his own roll number and marks is an object.
The Anatomy of an Object Creation
This one line does four things. Let us decode it:
| Part | Meaning |
|---|---|
| Student s1 | Declares a reference variable of type Student. It can point to Student objects. |
| new | Allocates memory (in heap) for a new Student object at run time. |
| Student() | Calls the constructor of the class to initialise the object. |
| = s1 | Makes s1 point to (refer to) that newly created object. |
Class vs Object — Question and Answer
| Point | Class | Object |
|---|---|---|
| What is it | A group of elements with common properties and behaviours | One individual element with physical behaviour |
| Nature | Virtual (a concept) | Real (exists in memory) |
| Encapsulation | Virtual encapsulation | Physical encapsulation |
| Relation to group | Generalization (the whole idea) | Specialization (one particular thing) |
| Another name | Blueprint / Model / Template | Instance of the class |
Memory trick: Class is the blueprint; Object is the building. One blueprint can build many buildings; each building is separate.
The 'Procedure to Write a Class' — Exam Pattern
The classic material gives a simple 4-step procedure that appears in exams:
- Declare the class with class keyword.
- Declare variables and methods in the class.
- In main(), create an object for the class.
- Access the class members through the reference variable.
And the mapping to remember: Entities (Student, Employee) → classes; entity data (sid, name) → variables; entity behaviours (add, search) → methods.
The Object Class — Grandparent of Every Class
Every class in Java directly or indirectly inherits from java.lang.Object. If your class extends nothing, Object is its direct super class; if it extends another class, Object is the indirect super class (multi-level).
The Object class gives 11 methods to every Java object:
| Method | Purpose |
|---|---|
| hashCode() | Returns a unique integer (hash code) for the object |
| toString() | Returns a text description of the object (class@hex) |
| getClass() | Returns the Class object of this object's type |
| clone() | Makes a copy of the object |
| equals(Object obj) | Compares two objects for equality |
| finalize() | Called by GC before destroying the object (deprecated) |
| wait() | Makes the thread wait (used in multithreading) |
| wait(long) | Wait with a timeout |
| wait(long, int) | Wait with a precise timeout |
| notify() | Wakes up one waiting thread |
| notifyAll() | Wakes up all waiting threads |
toString() — How Java prints an object
When you pass an object to System.out.println(obj), JVM internally calls obj.toString(). The Object class's version returns Class_Name@hashcode. Override toString() to print your own data.
- Class = blueprint; Object = real thing made from it (memory in heap).
- Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
- Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
- Constructor initialises the object; name = class name, no return type.
- this = current object; super = parent class; static = belongs to class; final = cannot change.
- abstract class cannot be instantiated; interface is a contract a class implements.
- Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).