how to declare abstract method in abstract class code example
Example 1: how to make abstract method in java
public abstract class Account {
protected int accountNumber;
protected Customer customerObj;
protected double balance;
public Account(int saccountNumber, Customer scustomerObj,double sbalance){
accountNumber = saccountNumber;
customerObj = scustomerObj;
balance = sbalance;
}
public abstract boolean withdraw(double amount);
}
public class SavingsAccount extends Account {
private double minimumBalance;
public SavingsAccount(int saccountNumber, Customer scustomerObj, double sbalance, double sminimumBalance) {
super(saccountNumber, scustomerObj, sbalance);
minimumBalance = sminimumBalance;
}
public boolean withdraw(double amount) {
if (balance() > minimumBalance && balance() - amount > minimumBalance) {
super.setBalance(balance() - amount);
return true;
} else {
return false;
}
}
}
Example 2: write a program in which an abstract class is being defined containg an abstract method omputer(int a, int b) and a non abstract method as well
abstract class Sum{
public abstract int compute(int a, int b);
public void disp(){
System.out.println("Method of class Sum");
}
}
class Demo extends Sum{
public int compute(int a, int b){
return a+b;
}
public static void main(String args[]){
Sum obj = new Demo();
System.out.println(obj.compute(3, 7));
obj.disp();
}
}