Classes and Objects
What is a Class in Java
A class is a blueprint or template used to create objects. It defines properties (variables) and behaviors (methods).
google.com, pub-8434817042454839, DIRECT, f08c47fec0942fa0
Example:
class Student {
String name;
int age;
}
String name;
int age;
}
What is an Object in Java
An object is an instance of a class. It represents a real-world entity and is used to access class properties and methods.
Example:
Student s1 = new Student();
s1.name = “Rahul”;
s1.age = 20;
s1.name = “Rahul”;
s1.age = 20;
Constructors in Java
A constructor is a special method used to initialize objects. It is automatically called when an object is created.
Types of Constructors
Default Constructor
class Student {
Student(){
System.out.println(“Default Constructor”);
}
}
Student(){
System.out.println(“Default Constructor”);
}
}
Parameterized Constructor
class Student {
String name;
Student(String name){
this.name = name;
}
}
String name;
Student(String name){
this.name = name;
}
}

