Java OOP with User Input
Building a Banking System
Why This Matters
Real-world systems like ATMs and mobile banking apps all follow the same fundamental flow:
User Input
Name, amount, account type
Processing
OOP structures handle logic
Output
Balance, confirmation, status
In Java, Scanner handles input and OOP principles structure the system — together they power the entire application.
The Complete Banking Program
This program brings together all four pillars of OOP in a single working example. Here is the full source code:
import [Link];
// ABSTRACTION
abstract class BankAccount {
private String name;
private double balance;
public BankAccount(String name, double balance) {
[Link] = name;
[Link] = balance;
}
// ENCAPSULATION
public String getName() { return name; }
public double getBalance() { return balance; }
public void setBalance(double b) { [Link] = b; }
public void deposit(double amount) {
balance += amount;
[Link]("Deposited: " + amount);
}
public abstract void withdraw(double amount);
}
// INHERITANCE + POLYMORPHISM
class SavingsAccount extends BankAccount {
public SavingsAccount(String name, double balance) { super(name, balance); }
@Override
public void withdraw(double amount) {
if (amount <= getBalance()) {
setBalance(getBalance() - amount);
[Link]("Savings Withdraw: " + amount);
} else {
[Link]("Insufficient balance!");
}
}
}
class CurrentAccount extends BankAccount {
public CurrentAccount(String name, double balance) { super(name, balance); }
@Override
public void withdraw(double amount) {
setBalance(getBalance() - amount);
[Link]("Current Withdraw (Overdraft Allowed): " + amount);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Name: ");
String name = [Link]();
[Link]("Enter Initial Balance: ");
double balance = [Link]();
BankAccount acc;
[Link]("1. Savings\n2. Current");
int choice = [Link]();
if (choice == 1) acc = new SavingsAccount(name, balance);
else acc = new CurrentAccount(name, balance);
[Link]("Enter Deposit: ");
[Link]([Link]());
[Link]("Enter Withdraw: ");
[Link]([Link]());
[Link]("Final Balance: " + [Link]());
[Link]();
}
}
Step 1 — The Scanner: Your Input Gateway
Scanner sc = new Scanner([Link]);
This single line opens a channel between the user and the program. Breaking it down:
Scanner [Link] sc
A built-in Java class for reading Represents the keyboard — the A reference variable — your handle to
console input standard input stream the Scanner object
Always import [Link] at the top of your file, or the Scanner class will not be recognized by the compiler.
Step 2 — Reading User Input
What Gets Stored
[Link]("Enter Name: ");
String name = [Link](); After the user types Karren and 3000:
[Link]("Enter Initial Balance: "); name = "Karren"
double balance = [Link](); balance = 3000.0
When the program runs, the user types their details at the console. These values are then passed into the object
Each method reads a different data type: constructor to initialize the account.
Method Reads Example
nextLine() Full text string "Karren"
nextDouble() Decimal number 3000.0
nextInt() Whole number 1
Step 3 — Declaring a Reference Variable
BankAccount acc;
What it IS What it is NOT Why it matters
A reference variable of type An actual object in memory — no Declaring with the parent type enables
BankAccount — a named slot that will constructor has run yet, no data is polymorphism — it can point to any
point to an object stored subclass
Step 4 — Dynamic Object Assignment
if (choice == 1)
acc = new SavingsAccount(name, balance);
else
acc = new CurrentAccount(name, balance);
The user's choice at runtime determines which subclass object
is created and assigned to acc. This is polymorphism in action
— the same variable behaves differently based on the object it
holds.
choice = 1 → acc points to a SavingsAccount object
with withdrawal limits.
choice = 2 → acc points to a CurrentAccount object
that allows overdraft.
Step 5 — Constructor Execution & Inheritance
new SavingsAccount(name, balance);
SavingsAccount BankAccount stores
Call constructor super(name,balance)
The super(name, balance) call inside the child constructor immediately invokes the parent's constructor — this is how inherited fields
like name and balance get initialized. After the chain completes, the object is fully ready with name = "Karren" and balance = 3000.
Encapsulation — Protecting Your Data
The balance field is declared private,
meaning it cannot be accessed or modified
directly from outside the class.
❌ Direct Access — WRONG ✅ Via Setter — CORRECT
[Link] = 5000; [Link](5000);
Compiler error — balance is private and inaccessible Controlled update through a public method with validation
potential
Encapsulation is the practice of hiding internal state and requiring all interaction to go through well-defined methods —
keeping your data safe from misuse.
Step 6 — Deposit in Action
[Link](deposit);
01 02 03
User enters deposit amount Method adds amount to balance Confirmation is printed
[Link]() reads the value from the Inside deposit(): balance += amount — the "Deposited: 500" confirms the transaction;
console (e.g., 500) private field is updated safely final balance becomes 3500
All four OOP pillars — Abstraction, Encapsulation, Inheritance, and Polymorphism — are at work in this single program.