In Java, classes and objects are the foundation of Object-Oriented Programming (OOP).
πΉ What is the Human Class?
A class in Java is like a blueprint.
In our case, the blueprint is a Human. Every human has:
π Name
π Age
β§ Gender
π Qualification
Hereβs the class definition:
class Human {
String name;
int age;
char gender;
String qualification;
public void getInformation() {
System.out.println(name + " " + age + " " + gender + " " + qualification);
}
}
πΉ Creating Objects from the Human Class
Once we have the Human class blueprint, we can create actual objects (humans).
Example:
public static void main(String ar[]) {
Human bharath = new Human();
bharath.name = "Bharath K";
bharath.age = 25;
bharath.gender = 'M';
bharath.qualification = "MSc Maths";
bharath.getInformation(); // Output: Bharath K 25 M MSc Maths
}
π Here, bharath is an object of the Human class.
We set values for his properties and then call the method getInformation() to display them.
πΉ Creating Multiple Objects
We can also create another object from the same class:
Human mani = new Human();
mani.getInformation(); // No values assigned, so it prints: null 0 οΏ½ null
β οΈ Since we didnβt assign values to Maniβs details, Java uses default values:
String β null
int β 0
char β blank (οΏ½)
πΉ Key Takeaways
Class = Blueprint (Human)
Object = Real Instance (Bharath, Mani)
Fields/Properties store data (name, age, gender, qualification)
Methods define behavior (getInformation())
If values are not set, Java assigns default values
Top comments (0)