Java Object-Oriented Programming Concepts and Best Practices
Java OOP Fundamentals
- True: If a class contains an abstract method, the class must be declared abstract.
- True: A class can implement multiple interfaces in Java.
- True: With a parent reference pointing to a child object, an overridden instance method is selected based on the actual object at runtime.
- False: A subclass cannot reduce the visibility of an overridden method.
- True: A
finalmethod cannot be overridden by a subclass.
Abstraction and Interfaces
Both interfaces and abstract classes support abstraction. Both can define abstract methods that subclasses must implement.
An abstract class can contain:
- Instance fields
- Constructors
- Normal methods
- Abstract methods
A class can extend only one class, but it can implement multiple interfaces. An interface mainly defines a contract that a class agrees to implement.
Static vs Instance Members
A static member belongs to the class, meaning there is one shared copy for all objects. For example, a static created counter can store the total number of accounts created.
An instance member belongs to an individual object. For example, each Account object should have its own balance.
Runtime Polymorphism
For a given reference, the actual object determines which method implementation runs:
- If the object is a
SavingsAccount,SavingsAccountmethods run. - If the object is an
Account,Accountmethods run. - Static counters track the total number of deposits made by all
Accountobjects because they are shared across all instances.
Design Patterns and Data Structures
Composition: I would use composition because a Transaction can contain a FeeCalculator with different implementations (e.g., fixed or percentage fees). This makes the system easier to extend and test.
Finding Accounts
- Find by account number: Use a
HashMapwith the account number as the key for fast lookup. - Top 5 accounts by balance: Use an
ArrayListand sort by balance in descending order. - Print all accounts by owner: Use an
ArrayListand sort alphabetically by owner name.
Java Implementation Examples
public static void writeAccountsToFile(List<Account> accounts) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("accounts_out.txt"))) {
bw.write("accountNo,type,owner,balance,extra");
bw.newLine();
for (Account acc : accounts) {
if (acc.getBalance() < 0) continue;
double extra = (acc instanceof SavingsAccount) ?
((SavingsAccount) acc).getInterestRate() :
((CheckingAccount) acc).getMonthlyFee();
bw.write(acc.getAccountNo() + "," + acc.getType() + "," +
acc.getOwner() + "," + acc.getBalance() + "," + extra);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}public static void splitAccounts(List<Account> accounts, List<SavingsAccount> low,
List<SavingsAccount> medium, List<SavingsAccount> high) {
for (Account acc : accounts) {
if (!(acc instanceof SavingsAccount)) continue;
SavingsAccount sa = (SavingsAccount) acc;
double rate = sa.getInterestRate();
if (rate < 0.02) low.add(sa);
else if (rate < 0.05) medium.add(sa);
else high.add(sa);
}
}public static void applyMonthlyUpdate(List<Account> accounts) {
for (Account acc : accounts) {
acc.monthlyUpdate();
}
}