what an abstract class do in java 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: abstract class java constructor
public class AbstractConstructorTest {
public static void main(String args[]) {
Server server = new Tomcat("Apache Tomcat");
server.start();
}
}
abstract class Server{
protected final String name;
public Server(String name){
this.name = name;
}
public abstract boolean start();
}
class Tomcat extends Server{
public Tomcat(String name){
super(name);
}
@Override
public boolean start() {
System.out.println( this.name + " started successfully");
return true;
}
}
Output:
Apache Tomcat started successfully