Java Class with Example जावा में क्लास उदाहरण सहित

A class is one of the most important concepts in Object-Oriented Programming. It acts as a template or blueprint that defines the structure and behavior of objects sharing common features. It holds all the properties and behaviors that an object may have. Instead of directly representing real-world entities, we define classes, and then create objects (instances) from them.

क्लास ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग की सबसे महत्वपूर्ण अवधारणाओं में से एक है। यह एक टेम्पलेट या ढांचा होता है जो समान गुणों वाले ऑब्जेक्ट्स को दर्शाता है। यह ऑब्जेक्ट की सभी विशेषताओं और व्यवहारों को संजोता है। हम किसी भी वास्तविक वस्तु को सीधे न दर्शाकर उसकी क्लास बनाकर ऑब्जेक्ट तैयार करते हैं।

For example, a Student class can represent attributes such as name, roll number, address, phone number, fees, and marks. An instance of the class represents an actual student. In Java, every class usually contains two main parts:

उदाहरण के लिए, एक Student क्लास, नाम, अनुक्रमांक, पता, मोबाइल नंबर, शुल्क, और अंक जैसी विशेषताओं का प्रतिनिधित्व कर सकती है। किसी क्लास का इंस्टैंस एक वास्तविक छात्र को दर्शाता है। जावा में प्रत्येक क्लास सामान्यतः दो भागों से बनी होती है:

a) Attributes

Attributes define the data or characteristics of an object. For example, a Motorcycle class may include attributes like color, style, chassis number, manufacturer, and price. These are typically defined using variables.

एट्रिब्यूट्स किसी वस्तु के गुणों को परिभाषित करते हैं। उदाहरण के लिए, एक Motorcycle क्लास में रंग, स्टाइल, चेसिस नंबर, निर्माता और कीमत जैसे गुण हो सकते हैं। इन्हें वेरिएबल्स द्वारा परिभाषित किया जाता है।

Example attributes for Student class:

int rollno;
String sname;
float marks;

b) Behavior

Behavior describes how an object acts or behaves using methods. For example, the Motorcycle class may have behaviors like startEngine(), stopEngine(), accelerate(), and changeGear().

बिहेवियर किसी वस्तु की क्रियाओं को परिभाषित करता है और इन्हें मेथड्स द्वारा लिखा जाता है। उदाहरण के लिए, Motorcycle क्लास में startEngine(), stopEngine(), accelerate() और changeGear() जैसे बिहेवियर्स हो सकते हैं।

Example method for Student class:

void sinfo() {
// some code
}

💡 Syntax of a Java Class

modifier class ClassName {
// attributes
// methods
}

✅ Example of a Simple Java Class

public class Student {
int rollno;
String sname;
float marks;
void sinfo() {
System.out.println("Roll No: " + rollno);
System.out.println("Name: " + sname);
System.out.println("Marks: " + marks);
}
}

📌 Java Program to Calculate Gross Salary of an Employee

import java.util.*;
public class EmployeeTest {
static class Employee {
double bs, HRA, TA, DA, gs;
void setSalary() {
Scanner s = new Scanner(System.in);
System.out.println("Enter Basic Salary of Employee:");
bs = s.nextDouble();
HRA = 0.05 * bs;
TA = 0.1 * bs;
DA = 0.2 * bs;
}
void getSalary() {
System.out.println("Basic Salary = " + bs);
System.out.println("HRA = " + HRA);
System.out.println("TA = " + TA);
System.out.println("DA = " + DA);
System.out.println("Gross Salary = " + grossSalary());
}
double grossSalary() {
if (bs >= 20000)
gs = bs + HRA + TA + DA + 5000;
else
gs = bs + HRA + TA + DA;
return gs;
}
}
public static void main(String args[]) {
Employee emp = new Employee();
emp.setSalary();
emp.getSalary();
}
}

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध