Nearby lessons

30 of 125

Java - Objects and Classes

📌 What You Will Learn
  • 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.

Trainer's Note: Notice s2.marks printed 0.0 — we did not set it, so it used the default value. Each object has its own separate copy of the variables. Changing s1 never affects s2.
In simple words: Every object gets its own separate copy of the variables. Two objects made from the same class never share data — changing one leaves the other completely untouched.
Example01
JCode Cell
1class Student { // class = blueprint
2 int rollNo; // properties (data)
3 String name;
4 double marks;
5 
6 void display() { // behaviour (method)
7 System.out.println(rollNo + " " + name + " " + marks);
8 }
9}
10 
11class Test {
12 public static void main(String[] args) {
13 Student s1 = new Student(); // object s1 from the blueprint
14 s1.rollNo = 101;
15 s1.name = "Rahul";
16 s1.marks = 88.5;
17 s1.display(); // call the method
18 
19 Student s2 = new Student(); // a second independent object
20 s2.rollNo = 102;
21 s2.name = "Priya";
22 s2.display();
23 }
24}
Output
101 Rahul 88.5 102 Priya 0.0

The Anatomy of an Object Creation

This one line does four things. Let us decode it:

PartMeaning
Student s1Declares a reference variable of type Student. It can point to Student objects.
newAllocates memory (in heap) for a new Student object at run time.
Student()Calls the constructor of the class to initialise the object.
= s1Makes s1 point to (refer to) that newly created object.
Example02
JCode Cell
1Student s1 = new Student();

Class vs Object — Question and Answer

PointClassObject
What is itA group of elements with common properties and behavioursOne individual element with physical behaviour
NatureVirtual (a concept)Real (exists in memory)
EncapsulationVirtual encapsulationPhysical encapsulation
Relation to groupGeneralization (the whole idea)Specialization (one particular thing)
Another nameBlueprint / Model / TemplateInstance 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:

MethodPurpose
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.

Example05
JCode Cell
1class A {
2 public String toString() { return "I am object of A"; }
3}
4 
5System.out.println(new A()); // calls toString() internally
📝 Key Takeaways
  • 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).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1