Premium Only Content

#22 Class and Object Practical in JAVA | Skyhighes | Lecture 22
Here's a practical demonstration of classes and objects in Java:
1. Create a Class:
Java
public class Calculator {
int num1;
int num2;
public int add() {
return num1 + num2;
}
public int subtract() {
return num1 - num2;
}
}
Use code with caution. Learn more
2. Create Objects of the Class:
Java
Calculator calc1 = new Calculator();
Calculator calc2 = new Calculator();
Use code with caution. Learn more
3. Access and Modify Object Attributes:
Java
calc1.num1 = 10;
calc1.num2 = 5;
calc2.num1 = 20;
calc2.num2 = 8;
Use code with caution. Learn more
4. Call Object Methods:
Java
int result1 = calc1.add(); // result1 will be 15
int result2 = calc2.subtract(); // result2 will be 12
System.out.println("Result of addition: " + result1);
System.out.println("Result of subtraction: " + result2);
Use code with caution. Learn more
Output:
Result of addition: 15
Result of subtraction: 12
Key Points:
Each object has its own distinct values for attributes (num1 and num2).
Methods (add() and subtract()) operate on the object's specific data.
Multiple objects of the same class can exist independently.
Additional Examples:
Create a Person class with attributes like name, age, and address, and methods like greet() and introduce().
Create a BankAccount class with attributes like accountNumber, balance, and methods like deposit(), withdraw(), and checkBalance().
Create a Car class with attributes like make, model, color, and methods like start(), stop(), and accelerate().
Experiment with different classes and objects to solidify your understanding of these fundamental OOP concepts in Java!
-
LIVE
BEK TV
23 hours agoTrent Loos in the Morning - 8/01/2025
338 watching -
13:08
Dad Saves America
12 hours ago $0.86 earnedTeachers Unions Play Politics While Students Lag Behind - Poisoning of the American Mind: Pt 4
11.4K9 -
20:05
Preston Stewart
13 hours ago $1.59 earnedCrimea Raid to Chasiv Yar Fight
10.9K8 -
8:11
Millionaire Mentor
15 hours agoTulsi Gabbard and Leavitt DOUBLE-TEAM Kaitlan Collins in FIERY Exchange
8.87K6 -
46:02
Coin Stories with Natalie Brunell
1 day agoWhat They’re Hiding About the Economy | Natalie Brunell with Danielle DiMartino Booth
42K7 -
12:12
GritsGG
14 hours ago23 Warzone Wins in a Row! (Cypher AR)
84.1K3 -
2:12:07
Side Scrollers Podcast
21 hours agoCULTURE SHIFT CAUSES MELTDOWNS + MASSIVE CENSORSHIP EFFORTS RAMP UP | SIDE SCROLLERS LIVE
26.8K12 -
11:25
Nikko Ortiz
1 day agoMost Painful Fails
57.9K30 -
43:55
pewculture
6 days ago $5.64 earnedIf the Purge was real, this is what we'd do... - EP#24
23.5K7 -
8:14
MattMorseTV
1 day ago $11.88 earnedTrump just DROPPED the HAMMER.
66.3K81