Abstract classes are those for which an instance or object can not be created. Hence, try to understand the cases where a class needs to be declared as abstract.
Sometimes, we can declare some methods (known as abstract methods) in a class that is to be defined in sub-classes. You may think that if the methods are defined in sub-classes why are they declared in super-class?
This is important because we can force the developers to ensure that the methods are defined in sub-classes. If the immediate sub-class is unable to define the method, the class needs to be declared as abstract again.
In another context, a class can be declared as an abstract class even if all the methods of the class are defined. This is required when we want to restrict the object creation of a class.
But a class must be declared as an abstract class if it contains any undefined (abstract) method.
In the following example, we have shown that class A is declared as an abstract class as it contains an abstract method. Class B is declared as an abstract class as inherited method method1() is not defined in it. Though a method is defined as class P, this class is declared as abstract, thus object can not be created for this class.
Sometimes, we can declare some methods (known as abstract methods) in a class that is to be defined in sub-classes. You may think that if the methods are defined in sub-classes why are they declared in super-class?
This is important because we can force the developers to ensure that the methods are defined in sub-classes. If the immediate sub-class is unable to define the method, the class needs to be declared as abstract again.
In another context, a class can be declared as an abstract class even if all the methods of the class are defined. This is required when we want to restrict the object creation of a class.
But a class must be declared as an abstract class if it contains any undefined (abstract) method.
In the following example, we have shown that class A is declared as an abstract class as it contains an abstract method. Class B is declared as an abstract class as inherited method method1() is not defined in it. Though a method is defined as class P, this class is declared as abstract, thus object can not be created for this class.
A.java
public abstract class A {
int x;
abstract void method1();
void method2() {
// logic
}
}
B.java
public abstract class B extends A {
int y;
void method3() {
// logic
}
}
C.java
public class C extends B {
int z;
void method1() {
// logic
}
void method4() {
// logic
}
}
P.java
public abstract class P {
void method1() {
// logic
}
}
Q.java
public class Q extends P {
void method2() {
// logic
}
}
TestMain.java
public class TestMain {
public static void main(String[] args) {
// A a = new A(); // this an abstract class
// B b = new B(); //this is an abstract class
C c = new C();
// P p = new P(); // this is an abstract class
Q q = new Q();
}
}
Case Study
In the example discussed in the previous chapter, you can create an object of any class. But an object of the Account class or LoanAccount class should be restricted. So there is a way to restrict the instance creation of a class. If you want to restrict object creation, the class should be defined as an abstract class.
Moreover, at the time of defining a class, you may include the signature of some methods that can not be defined in that class. Then this method is known as the abstract method. If a class contains any abstract method, the class must be defined as an abstract class.
In both of HomeLoanAccount and EducationLoanAccount classes, we have defined updateLoanAmountOnInterest method. Every loan account class should have this method. So, we should declare this method in LoanAccount class, and define the method in sub-classes.
So the example of the previous chapter can be redesigned as follow:
Moreover, at the time of defining a class, you may include the signature of some methods that can not be defined in that class. Then this method is known as the abstract method. If a class contains any abstract method, the class must be defined as an abstract class.
In both of HomeLoanAccount and EducationLoanAccount classes, we have defined updateLoanAmountOnInterest method. Every loan account class should have this method. So, we should declare this method in LoanAccount class, and define the method in sub-classes.
So the example of the previous chapter can be redesigned as follow:
Account.java
public abstract class Account {
private int _accountNo;
private String _customerName;
private String _customerAddress;
private String _branchName;
private String _branchAddress;
private float _amount;
private float _rateOfInterest;
public int get_accountNo() {
return _accountNo;
}
public void set_accountNo(int _accountNo) {
this._accountNo = _accountNo;
}
public String get_customerName() {
return _customerName;
}
public void set_customerName(String _customerName) {
this._customerName = _customerName;
}
public String get_customerAddress() {
return _customerAddress;
}
public void set_customerAddress(String _customerAddress) {
this._customerAddress = _customerAddress;
}
public String get_branchName() {
return _branchName;
}
public void set_branchName(String _branchName) {
this._branchName = _branchName;
}
public String get_branchAddress() {
return _branchAddress;
}
public void set_branchAddress(String _branchAddress) {
this._branchAddress = _branchAddress;
}
public float get_amount() {
return _amount;
}
public void set_amount(float _amount) {
this._amount = _amount;
}
public float get_rateOfInterest() {
return _rateOfInterest;
}
public void set_rateOfInterest(float _rateOfInterest) {
this._rateOfInterest = _rateOfInterest;
}
}
SavingsAccount.java
public class SavingsAccount extends Account {
public void payAnnualInterest() {
set_amount(get_amount() + get_amount() * get_rateOfInterest() / 100);
}
public void deposit(float amount) {
set_amount(get_amount() + amount);
}
public void withdrawl(float amount) {
set_amount(get_amount() - amount);
}
public float getAccountBalance() {
return get_amount();
}
}
LoanAccount.java
public abstract class LoanAccount extends Account {
public float getRemainingLoanAmount() {
return get_amount();
}
public void rePayment(float amount) {
set_amount(get_amount() - amount);
}
public abstract void updateLoanAmountOnInterest(int noOfYear);
}
HomeLoanAccount.java
public class HomeLoanAccount extends LoanAccount {
// update loan amount based on compound interest
public void updateLoanAmountOnInterest(int noOfYear) {
set_amount(get_amount() + get_amount() * (float) Math.pow(get_rateOfInterest() / 100, noOfYear));
}
}
EducationLoanAccount.java
public class EducationLoanAccount extends LoanAccount {
// update loan amount based on simple interest
public void updateLoanAmountOnInterest(int noOfYear) {
set_amount(get_amount() + get_amount() * get_rateOfInterest() * noOfYear / 100);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.