Liskov's Substitution Principle
by Samudra Gupta
That Java is an Object Oriented
language does not necessarily mean that the code written in Java is always
Object Oriented. If this statement surprises you, this series is for you. In
this series, I will try to demonstrate some design aspects, both good and bad,
that are the key to well written software in Java. The first of these is the
Liskov’s Substitution Principle (LSP), which will lead to the Design by Contract and
Dependency Inversion Principle. We will then see how all of them conform to one
most vital principle of OO design, the Open-Closed Principle.
Liskov’s Substitution Principle
The Liskov’s Substitution Principle
provides a guideline to sub-typing any existing type. Stated formally it reads:
If for each object o1 of type S there is an object o2 of type T such that for
all programs P defined in terms of T, the behaviour of P is unchanged when o1 is
substituted for o2 then S is a subtype of T.
In a simpler term, if a
program module is using the reference of a Base class, then it should be able to
replace the Base class with a Derived class without affecting the functioning of
the program module.
An example
When I was in the beginning of my IT career, I was working on
a banking project and I was asked to design an account-handling module. To
simplify the scenario, let us assume that I had two types of accounts to handle.
One is the "Current Account" and the other is a special type of current account
with a better interest rate, but with some restrictions that the account cannot
be closed before a certain time period. Having done the preliminary analysis, I
decided to come up with two account objects "CurrentAccount" and
"SpecialCurrentAccount," and also decided to write another class that would offer
the interfaces to operate on these "XXXAccount" objects. I also analysed that
the "SpecialCurrentAccount" and "CurrentAccount" share lot in common.
Delighted with such a straightforward relationship, I came up with the following
class diagram. The SpecialCurrentAccount is a sub-type of the CurrentAccount.
The program
specification for the module specified the following constraints.
- The closing of Current Account should check for a balance greater than
zero. If satisfied, proceed to close the account.
- The closing of Special Current Account should check for a balance greater
than zero and also that the minimum period for the account is covered. If satisfied,
proceed to close the account.
From the above specification and my own
class diagram, I decided that I will override a closeAccount() method in the
derived SpecialAccount class. The following programs CurrentAccount.java
(Listing 1) and SpecialCurrentAccount.java (Listing 2) describe the designed
classes.
/*
* Account.java
*
*/
package sam.oo.bad;
public class CurrentAccount {
/** Holds value of property balance. */
protected int balance;
/** Holds value of property period. */
protected int period;
/** Creates a new instance of Account */
public CurrentAccount(int balance, int period) {
this.balance = balance;
this.period = period;
}
/**
* open a current account with the given balance
*/
public boolean openAccount(int balance)
{
this.balance = balance;
return true;
}
/**
*closes the account
*/
public boolean closeAccount()
{
if(balance >0)
return true;
else
return false;
}
/** Getter for property balance.
* @return Value of property balance.
*/
public int getBalance() {
return this.balance;
}
/** Setter for property balance.
* @param balance New value of property balance.
*/
public void setBalance(int balance) {
this.balance = balance;
}
/** Getter for property period.
* @return Value of property period.
*/
public int getPeriod() {
return this.period;
}
/** Setter for property period.
* @param period New value of property period.
*/
public void setPeriod(int period) {
this.period = period;
}
}
Listing 1 CurrentAccount.java
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|