Content ITV PRO
This is Itvedant Content department
Learning Outcome
5
Use constructors to initialize objects
4
Create an object using new
3
Differentiate class vs object
2
Understand what an object is
1
Understand what a class is in Java
6
Use basic modifiers (public/private, static/final)
Variables: Know how to store data like int, double, String
Basic Java Syntax: Know how to write class, main method, and print output.
Methods (Functions): Understand how methods work, how to pass values and return results.
Arrays: Understand how methods work, how to pass values and return results
Think of a bakery where cakes are prepared every day
The bakery does not keep just one recipe.
It stores many related recipes together in one place so that they are easy to find and manage.
Once a cake is ready, it has specific characteristics, such as:Flavor,Size
A cake is not just something to look at.
Different actions can be performed on it:
The cake can be cut
The cake can be eaten
After the cake is completely eaten, the plate is no longer needed, so it is thrown away.
Similarly in java,
The recipe represents a class, as it defines how something should be created.
Each cake made using the recipe represents an object, as it is a real instance.
The flavor and size represent variables, as they store the state of the cake.
Actions like cutting and eating represent methods, as they define behavior.
Clearing the plate after the cake is finished represents garbage collection, as unused items are removed automatically.
The initial decisions made while baking represent a constructor, as it initializes the cake.
The bakery that stores many recipes together represents a package, as it organizes related classes.
An object is a real instance created from a class. It uses the class blueprint to store actual data and perform specific actions.
Real Example: ChocolateCake and VanillaCake are objects created from the CakeRecipe class.
Bakery Analogy: A cake recipe is like a class—it defines ingredients and baking steps but isn't the actual cake itself.
A class is a template that defines what an object will contain (variables) and what actions it can perform (methods).
Class Syntax
class ClassName {
// Fields (variables)
// Methods (functions)
}ClassName variable =
new ClassName();Object Creation
class Cake {
String flavor;
void bake() {
System.out.println("Baking " + flavor + " cake");
}
}
public class Main {
public static void main(String[] args) {
Cake c1 = new Cake();
c1.flavor = "Chocolate";
c1.bake();
}
}
A constructor is a special method used to initialize objects. It runs automatically when an object is created.
Key Rules
No return type, not even void.
Name must be the same as the class name.
Executed automatically upon using the new keyword.
class ClassName {
// Constructor
ClassName() {
// initialization code
}
}
Syntax
class Student {
String name;
int age;
// Constructor
Student(String n, int a) {
name = n;
age = a;
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Rahul", 20);
}
}
Example:
Default Constructor
Provided automatically by the compiler
Sets default values
class Student {
// Compiler adds: Student() {}
}
No-Argument Constructor
Written by the programmer
Used for custom initialization
class Student {
Student() {
System.out.println("Object created");
}}
Parameterized Constructor
Accepts arguments for initialization
Avoids extra setter calls
class Student {
Student(String name) {
System.out.println("Name:"+ name);
}}
Copy Constructor
Creates a new object using another object’s data
Must be defined manually
class Student {
Student(Student s) {
System.out.println("Object copied");
}}
A destructor is a special method used to clean up resources before an object is destroyed.
Unlike C++, Java does not use manual destructors.
Java uses Garbage Collection (GC) to automatically remove unused objects.
The finalize() method exists for cleanup but is deprecated and rarely used.
class Cake {
protected void finalize() {
System.out.println("Object is destroyed");
}
}
Example:
Access modifiers control who can see or use variables and methods in a class.
Private
public
Protected
Default
Non-access modifiers change the behavior of variables, methods, or classes.
Static
Final
Summary
5
Modifiers control access and behavior
4
Java uses garbage collection instead of destructors
3
Objects store data and perform actions
2
An object is a real example of a class
1
A class is a blueprint
Quiz
How do you create an object in Java?
A. ClassName obj;
B. ClassName obj = new ClassName();
C. new ClassName;
D. obj = ClassName();
How do you create an object in Java?
A. ClassName obj;
B. ClassName obj = new ClassName();
C. new ClassName;
D. obj = ClassName();
Quiz-Answer
By Content ITV