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!
-
2:35:23
FreshandFit
6 hours agoWhy Black Men Don't Date Black Women Debate
26.5K30 -
2:03:42
Inverted World Live
10 hours agoBigfoot Corpse Coming to the NY State Fair | Ep. 94
100K24 -
6:16:23
SpartakusLIVE
11 hours ago$1,000 Pistol Challenge || #1 ENTERTAINER of The EONS Eradicates BOREDOM
75.6K2 -
2:33:37
TimcastIRL
8 hours agoTrump Orders Review of Smithsonian For Being Woke & Out of Control | Timcast IRL
177K67 -
3:09:10
Barry Cunningham
12 hours agoPRESIDENT TRUMP HAS TAKEN THE MONSTER AWAY FROM THE LEFT! HORROR STORIES WON'T WORK ANYMORE!
78.9K80 -
1:29:55
WickedVirtue
6 hours agoLate Night Fortnite w/ Friends
48.8K -
3:34:06
This is the Ray Gaming
6 hours ago $0.79 earnedCould you be? Would you be? Won't you be my RAYBOR? | Rumble Premium Creator
29.4K -
1:46:52
JahBlessGames
7 hours ago🎉Come een' and come tru' - VIBES | MUSIC | GAMES
50.9K2 -
38:47
MattMorseTV
9 hours ago $14.15 earned🔴Tulsi just CLEANED HOUSE.🔴
67.1K120 -
6:24:06
Reolock
10 hours agoWoW Classic Hardcore | WE'RE BACK!!
30.2K1