DEV Community

KIRAN RAJ
KIRAN RAJ

Posted on

Empowering Minds: Insights from Payilagam Institute

Java Classes and Objects: Building Blocks of OOP

What is a Class?
A class is a blueprint that defines properties and behaviors. Let's consider a Human class with attributes like name,age,gender, and qualification.

Human Class Example
class Human {
String name;
int age;
char gender;
String qualification;

public void getInformation() {
    System.out.println(name + " " + age + " " + gender + " " + qualification);
}
Enter fullscreen mode Exit fullscreen mode

}
Creating Objects
We can create objects from the Human class, like bharath and mani. Each object has its own set of attributes and behaviors.

Example Code
public static void main(String[] args) {
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

Human mani = new Human();
mani.getInformation(); // Output: null 0 � null (default values)
Enter fullscreen mode Exit fullscreen mode

}
Key Takeaways:

  • Class = Blueprint (defines properties and behaviors)
  • Object = Real instance (e.g., bharath, mani)
  • Fields/Properties store data (e.g., name, age)
  • Methods define behavior (e.g., getInformation())
  • Java assigns default values if properties are not set (e.g., null, 0, �)

Top comments (0)