advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Liskov's Substitution Principle :

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
 

How to Add Java Applets to Your Site

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.

 BlackBerry Application Development Resources
 Microsoft Visual Studio 2010 Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow
Facebook Makes Major PHP Push With HipHop
Free Ride Over for Microsoft Azure Users
Drupal Opens the Garden to Boost CMS
Oracle Talks Plans for Linux, Solaris
Azure Makes Cloud Computing Innovation Safe
Red Hat's JBoss Looks Ahead
Microsoft Readies Two Windows Phone Systems?

Apple Surveying iPhone Developers? Happiness With The App Store
HTML 5 Leaves Client Storage Open to Web Attacks
Basic Market Forecasting with Encog Neural Networks
Location-Aware App Review
The Future of Web Content -- HTML5, Flash, and Mobile Apps
Moonlight 3.0 Preview Offered For Rich Internet Apps on Linux and Unix
Why a Moderator is Key in the Engineering Review Process, Part II
Windows 7 Features Your Clients Will Need on Day One
What Your Clients Will Ask About Windows 7
Melissa Data Helps Developers Improve the Quality of Business Data

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers