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!
-
14:41
Nate The Lawyer
1 day ago $0.66 earnedWhy Hasn’t Raja Jackson Been Arrested Yet?
4.04K13 -
6:04
Buddy Brown
2 days ago $1.21 earnedEugene’s SICK Newspaper Cover is a WARNING to Us All! | Buddy Brown
6.99K9 -
16:23
Actual Justice Warrior
1 day agoMom Sucker Punched By Repeat Offender In Chicago
11K29 -
56:28
Professor Nez
1 day ago🚨Not Only is Epstein NOT Going Away… It Just Got Worse!
25K25 -
8:09
MattMorseTV
17 hours ago $11.19 earnedTrump scores 17th CONSECUTIVE Supreme Court VICTORY.
76.7K60 -
2:11:25
Side Scrollers Podcast
19 hours agoUK JAILS TV WRITER FOR WRONG THINK + TWITCH ALLOWS CYBERSTALKING + MORE | SIDE SCROLLERS LIVE
34.9K10 -
10:47
Nikko Ortiz
1 day agoThese Tik Tok Clips Are Extremely Painful...
55.3K7 -
13:43
GritsGG
15 hours agoI Made a Brand New Warzone Account! Will I Get Bot Lobbies?
12.4K1 -
1:09:23
The HotSeat
15 hours agoTrump’s Parade = Mocked, China’s Parade = Praised: Leftist Hypocrisy EXPOSED
38.7K33 -
LIVE
Lofi Girl
2 years agoSynthwave Radio 🌌 - beats to chill/game to
237 watching