1Sometimes we may come across a situation where we cannot provide
2implementation to all the methods in a class. We want to leave the
3implementation to a class that extends it. In such case we declare a class
4as abstract.To make a class abstract we use key word abstract.
5Any class that contains one or more abstract methods is declared as abstract.
6If we don’t declare class as abstract which contains abstract methods we get
7compile time error.
8
9 1)Abstract classes cannot be instantiated
10 2)An abstarct classes contains abstract method, concrete methods or both.
11 3)Any class which extends abstarct class must override all methods of abstract
12 class
13 4)An abstarct class can contain either 0 or more abstract method.
1public abstract class Account { //abstract class //perent class
2 protected int accountNumber;
3 protected Customer customerObj;
4 protected double balance;
5 //constructor
6 public Account(int saccountNumber, Customer scustomerObj,double sbalance){
7 accountNumber = saccountNumber;
8 customerObj = scustomerObj;
9 balance = sbalance;
10 }
11 // abstract Function
12 public abstract boolean withdraw(double amount);
13}
14
15public class SavingsAccount extends Account { // child class
16 private double minimumBalance;
17 // constructor
18 public SavingsAccount(int saccountNumber, Customer scustomerObj, double sbalance, double sminimumBalance) {
19 super(saccountNumber, scustomerObj, sbalance);
20 minimumBalance = sminimumBalance;
21 }
22 // Implementation of abstract function in child class
23 public boolean withdraw(double amount) {
24 if (balance() > minimumBalance && balance() - amount > minimumBalance) {
25 super.setBalance(balance() - amount);
26 return true;
27 } else {
28 return false;
29 }
30 }
31}
32
33