상속에 대한 간단한 코딩 예제

발행: (2026년 3월 16일 오전 07:09 GMT+9)
2 분 소요
원문: Dev.to

Source: Dev.to

은행 시스템을 위한 간단한 코딩 예제

package bank.task;

public class BankAccount {

    int accountNumber;
    double balance;

    public void deposit(double depositAmount, int accNo, String AccType) {
        double total = balance + depositAmount;
        System.out.println("An amount of Rs." + depositAmount + "  has been deposited succesfully ");
        System.out.println("The total amount in your " + AccType + "  Account of   Account number" + accNo + " is " + total);
    }

    protected void withdraw(double withdrawAmount, int accNo, String AccType) {
        double total = balance - withdrawAmount;
        System.out.println("An amount of Rs." + withdrawAmount + " has been withdrawn succesfully ");
        System.out.println("The total amount in your " + AccType + "  Account of  your Account number" + accNo + " is " + total);
    }
}

class SavingAccount extends BankAccount {
    double interestRate;

    public void displayInterest(double rate) {
        System.out.println("The interest rate is" + rate);
    }
}

class CurrentAccount extends BankAccount {
    int limit = 2000;

    public void payPenalty(float balance) {
        if (balance < limit)
            System.out.println("Your balance is less than Rs. " + limit + " . Please pay a penalty of Rs.500");
        else
            System.out.println("Your balance is above than Rs. " + limit + " .NO penalty");
    }
}

class User {
    public static void main(String args[]) {
        SavingAccount sa = new SavingAccount();
        CurrentAccount ca = new CurrentAccount();

        sa.balance = 10000.00;
        sa.deposit(2000.00, 1234567, "Savings");
        sa.withdraw(2345.00, 1234567, "Savings");
        sa.displayInterest(6.7F);

        ca.balance = 1900;
        ca.payPenalty((float) ca.balance);
    }
}
0 조회
Back to Blog

관련 글

더 보기 »

JavaScript OOP: 설계도에서 현실로

!JavaScript OOP: 청사진에서 현실로 표지 이미지 https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A...

JavaScript에서 객체지향 프로그래밍 이해하기

JavaScript 시리즈 9번째 블로그 안녕하세요, 독자 여러분 👋, JavaScript 시리즈의 9번째 블로그에 다시 오신 것을 환영합니다. 당신이 자동차 제조업체라고 상상해 보세요. 당신은…