Classes and Objects in Java - Simple Ga Nerchukundam
Welcome to LearnTechSoft - Java Course Topic 5
Yesterday manam OOPs Concepts gurinchi nerchukunnam. OOPs ante Object Oriented Programming. Ippudu aa OOPs ki heart enti ante - Class and Object.
Java nerchukovali ante Class and Object ardham aithe, 70% Java ardham ayinatte. Interview lo kuda first adige question ide - "What is difference between Class and Object?"
Ee article lo manam Class ante enti, Object ante enti, vatini ela create cheyali, real-life examples tho, Java code tho full ga nerchukundam.
1. Class Ante Enti?
Class ante oka Blueprint / Plan / Design.
Simple ga cheppalante - Illu kattalante mundu Engineer oka plan vestadu kada? Aa plan lone - entha rooms, ekkada kitchen, ekkada hall ani untundi. Aa plan ni chusi 100 illu kattachu.
Java lo Class kuda anthe. Class lo manam cheptham - ee type object ki em properties untayi, em behaviours untayi ani.
Example: Student Class anukondi.
Student ki em untundi?
- Properties: name, roll number, marks
- Behaviours: study(), writeExam(), payFees()
Ippudu ee Student Class ni chusi, manam 1000 mandi students ni create cheyachu. Andariki same design, kani values veru veru ga untayi.
Class lo em untundi?
1. Variables - Data ni store cheyadaniki
2. Methods - Pani cheyadaniki
3. Constructors - Object ni create chesetappudu value ivvadaniki
Class ni manam direct ga use cheyalem. Class nundi Object create cheste ne adi pani chesthundi.
2. Object Ante Enti?
Object ante Class nundi create chesina Real Entity.
Plan chusi kattina Illu lanti di Object. Plan okkate, illu chala untayi.
Student Class nundi manam oka Object create chesthe - adi oka nijamaina student avuthundi. Example: Ramesh ane student.
Ramesh ki name = "Ramesh", roll number = 101, marks = 85 ani untundi. Suresh ane inkoka object ki values veru ga untayi.
Object ki 3 vishayalu untayi:
1. State: Object yokka present data. Example: name, age
2. Behaviour: Object em cheyagaladu. Example: study, play
3. Identity: Prathi object ki oka pratyeka identity untundi. Java lo danini reference tho gurthistham.
Gurthupettukondi - Class ante design, Object ante aa design tho tayaru chesina nijamaina vastu.
3. Class and Object Difference - Interview Point
Class:
- Blueprint / Design
- Memory lo place theesukodu
- Okasari matrame rayali
- Example: Car Design Plan
Object:
- Class nundi create chesina real thing
- Memory lo place theesukuntundi
- Enni objects aina create cheyachu
- Example: Nee intlo unna Car
Interview lo ila chepthe interviewer ki nuvvu concept clear ga unnavu ani ardham avuthundi.
4. Java lo Class Ela Rayali?
Ippudu practical ga chuddam. Student Class ela rayalo:
class Student {
// Variables - Properties
String name;
int rollNumber;
int marks;
// Method - Behaviour
void study() {
System.out.println(name + " is studying");
}
void display() {
System.out.println("Name: " + name);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Marks: " + marks);
}
}
Ee code lo Class peru Student. Dantlo 3 variables, 2 methods unnayi. Ippati varaku manam object create cheyaledu, kabatti idi just oka plan matrame.
5. Java lo Object Ela Create Cheyali?
Object create cheyadaniki manam new keyword vadatham.
public class Main {
public static void main(String[] args) {
// Object creation
Student s1 = new Student();
Student s2 = new Student();
// Values assign cheyadam
s1.name = "Ramesh";
s1.rollNumber = 101;
s1.marks = 85;
s2.name = "Suresh";
s2.rollNumber = 102;
s2.marks = 92;
// Methods call cheyadam
s1.display();
s1.study();
s2.display();
s2.study();
}
}
Output:
Name: Ramesh
Roll Number: 101
Marks: 85
Ramesh is studying
Name: Suresh
Roll Number: 102
Marks: 92
Suresh is studying
Chusara? Oke Class nundi rendu veru veru Objects create chesam. Rendu kalisi pani chesthunnayi, kani vatiki values veru.
6. Constructor Ante Enti?
Prathi sari object create chesi, s1.name = ... ani value ivvadam kastam kada? Danikosame Constructor vasthundi.
Constructor ante Object create ayye time lone value pette oka special method.
class Student {
String name;
int rollNumber;
// Constructor
Student(String n, int r) {
name = n;
rollNumber = r;
}
void display() {
System.out.println(name + " - " + rollNumber);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Ramesh", 101);
Student s2 = new Student("Suresh", 102);
s1.display();
s2.display();
}
}
Ila chesthe code chala short ga, neat ga untundi. Interview lo Constructor gurinchi adigithe - "Constructor is used to initialize the object at the time of creation" ani cheppandi.
7. Real Life Examples - Easy Ga Gurthu Pettukondi
1. Car Example:
Class = Car Design
Object = Swift, Innova, BMW - anni Car Class nundi vachinave
2. Mobile Example:
Class = Mobile Design
Object = Nee Mobile, Naa Mobile
3. Bank Example:
Class = Bank Account
Object = Nee Account, Naa Account
Ee examples tho chepthe evarikaina easy ga ardham avuthundi.
8. Beginners Chese Common Mistakes
1. Class name ni small letter tho rayadam - Java lo Class name eppudu Capital letter tho modalu pettali. Example: Student, Car
2. Object create cheyakunda method ni call cheyadam - Object lekunda method pani cheyadu.
3. new keyword marchipovadam - Object create cheyali ante new tappakunda kavali.
4. Main method lekunda program run cheyadam - Java program run avvalante main method undali.
Ee mistakes cheyakunda unte mee Java journey chala easy ga untundi.
9. Practice Cheyandi - Homework
Ee article chadivaka ee 2 programs mee laptop lo try cheyandi:
Task 1: Book ane Class create cheyandi. Dantlo title, author, price undali. Rendu objects create chesi display cheyandi.
Task 2: Car ane Class create cheyandi. Dantlo brand, color, speed undali. Constructor use chesi values ivvandi.
Ee rendu cheste, Class and Object concept meeku 100% clear avuthundi.
Conclusion
Friends, ee roju manam nerchukunnam:
- Class ante Blueprint, Object ante Real Thing
- Class nundi enni objects aina create cheyachu
- Constructor tho object ni easy ga initialize cheyachu
Next Topic lo manam Inheritance in Java gurinchi nerchukundam - OOPs lo most important pillar.
Ee article nachithe mee friends ki share cheyandi. Doubt unte comment lo adagandi.
Next Article: Inheritance in Java - Explained in Simple Words