Design a Custom Class
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-20
Problem Statement
Design and implement a custom class in an object-oriented language (such as Java, Python, or C++) that models a real-world entity.
The class should include:
1.Encapsulation — private fields with public getters/setters.
2.Constructors — to initialize objects with different configurations.
3.Validation logic — to ensure object state remains valid.
4.Meaningful methods — that represent behavior related to the entity.
5.Overrides (optional) — like toString(), equals(), or hashCode() to support comparison or printing.
✅ Functional Requirements
•The class must have at least 3–5 attributes (fields).
•It must support:
•Object creation using constructors.
•Attribute updates through setters.
•Information retrieval through getters.
•Include at least two meaningful methods representing operations or actions.
•Include a validation mechanism (e.g., reject invalid inputs).
•Demonstrate encapsulation and data hiding.
⚙️ Non-Functional Requirements
Aspect with Description
Reusability
Should be easy to reuse or extend.
Encapsulation
Data must not be directly accessible outside the class.
Readability
Clear and self-explanatory method names.
Maintainability
Should be simple to modify or add attributes.
💡 Example Scenarios ------------------------------------
Design a Custom Class — BankAccount
•Fields: accountNumber, accountHolderName, balance
•Methods: deposit(), withdraw(), getBalance()
•Validation: Prevent negative balance or invalid deposits.
Design a Custom Class — Book
•Fields: title, author, isbn, price
•Methods: applyDiscount(), displayDetails()
•Validation: Ensure price > 0 and non-empty title/author.
Design a Custom Class — Student
•Fields: name, id, marks, grade
•Methods: calculateGrade(), printReport()
•Validation: Marks between 0 and 100.
🧱 Example — BankAccount Class
1
2public class BankAccount {
3 private String accountNumber;
4 private String accountHolder;
5 private double balance;
6
7 public BankAccount(String accountNumber, String accountHolder, double initialBalance) {
8 if (initialBalance < 0) throw new IllegalArgumentException("Initial balance cannot be negative");
9 this.accountNumber = accountNumber;
10 this.accountHolder = accountHolder;
11 this.balance = initialBalance;
12 }
13
14 public void deposit(double amount) {
15 if (amount <= 0) throw new IllegalArgumentException("Deposit amount must be positive");
16 balance += amount;
17 }
18
19 public void withdraw(double amount) {
20 if (amount > balance) throw new IllegalArgumentException("Insufficient funds");
21 balance -= amount;
22 }
23
24 public double getBalance() { return balance; }
25
26 @Override
27 public String toString() {
28 return "Account[" + accountNumber + "] Holder: " + accountHolder + " | Balance: $" + balance;
29 }
30}🧠 Interview Discussion Points
•How does the class demonstrate encapsulation and data hiding?
•How would you handle invalid data?
•How can the class be extended (e.g., subclassing SavingsAccount)?
•When should you override equals() / hashCode()?
•How would you test the class?
💬 Final Interview Prompt
Design a custom class (for example, BankAccount, Book, or Student) that models a real-world entity.
Implement attributes, methods, constructors, and validation to ensure encapsulation and correctness of object behavior.
Be prepared to discuss design choices, validation, and potential extensions.