Bird
0
0

You want to convert this procedural code into OOP style:

hard📝 Application Q8 of 15
Java - Object-Oriented Programming Concepts
You want to convert this procedural code into OOP style:
int balance = 1000;
void deposit(int amount) {
  balance += amount;
}
deposit(500);
System.out.println(balance);

Which is the best OOP approach?
AKeep balance as a global variable and deposit as a static method.
BCreate a BankAccount class with balance attribute and deposit method.
CUse a function outside any class to modify balance.
DDeclare balance as final and deposit as a constructor.
Step-by-Step Solution
Solution:
  1. Step 1: Identify OOP encapsulation

    OOP groups data and behavior inside classes.
  2. Step 2: Create class with balance and deposit method

    This encapsulates balance and deposit logic properly.
  3. Final Answer:

    Create a BankAccount class with balance attribute and deposit method. -> Option B
  4. Quick Check:

    Encapsulate data and methods = B [OK]
Quick Trick: OOP bundles data and methods in classes [OK]
Common Mistakes:
  • Using global variables instead of class attributes
  • Making deposit static without object context
  • Misusing final keyword for balance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes