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 :

Here is the code for the SpecialCurrentAccount.

/*
 * SpecialCurrentAccount.java
 *
 */

package sam.oo.bad;

public class SpecialCurrentAccount extends CurrentAccount{
    
    /** Holds value of property defaultPeriod. */
    private int defaultPeriod;
    
    /** Creates a new instance of SavingsAccount */
    public SpecialCurrentAccount(int balance, int period) {
        super(balance, period);
    }
    
    public boolean closeAccount()
    {
        if(balance>0 && period>defaultPeriod)
            return true;
        else
            return false;
    }
    
    /** Getter for property defaultPeriod.
     * @return Value of property defaultPeriod.
     */
    public int getDefaultPeriod() {
        return this.defaultPeriod;
    }
    
    /** Setter for property defaultPeriod.
     * @param defaultPeriod New value of property defaultPeriod.
     */
    public void setDefaultPeriod(int defaultPeriod) {
        this.defaultPeriod = defaultPeriod;
    }
    
}


Listing 2 SpecialCurrentAccount.java

Also I decided to offer an interface through another class to operate on these account objects, which looked like:

public void closeAnAccount(CurrentAccount ac)
    {
        System.out.println("Account close result: "+ac.closeAccount());
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        AccountTest test = new AccountTest();
        
        CurrentAccount ac = new CurrentAccount(100,2);
        SpecialCurrentAccount sac = new SpecialCurrentAccount(200,5);
        
        test.closeAnAccount(ac);
        test.closeAnAccount(sac);
        
        
    }

Everything went fine, and I was happy, until a user of this module discovered an unexpected behaviour with the system. When he tried to do the above, the result was contrary to his expectation.

Account close result: true
Account close result: false

The closing of a SpecialCurrentAccount object failed and the user has no clue why, without looking into the source code of the SpecialCurrentAccount class. But looking into interface closeAnAccount(CurrrentAccount), he is justified to make an assumption that any type of CurrentAccount object will behave in a similar manner. But in reality it did not. That made me think a while and I came to the following conclusions:

  • While designing the class hierarchy I have violated the LSP.
  • My overridden method closeAccount() in the derived class SpecialCurrentAccount had broken the module by not allowing a base class CurrentAccount object to be replaced by derived class object SpecialCurrentAcount.
  • By creating the above hierarchy I had expected the user to know about the internal implementation of both the account classes before he can be assured of a guaranteed behaviour of the module. Clearly, this is not desirable and often he might not have access to the source code to do so.

Design by Contract

The above discussion leads us to the discussion of the Design by Contract principle. In Design by Contract, each method in a class can have a pre-condition and a post-condition attached to it. The pre-condition defines the criteria to be met before the method offers a certain behaviour and the post-condition is the state or behaviour offered by the method once the pre-conditions are met. Following that, if we note down the pre-conditions and post-conditions followed by the CurrentAccount and the SpecialCurrentAccount classes, they will look like the following:

CurrentAccount.java

/**
* pre condition: the balance >0
* post-condition: the account is closed
**/
public boolean closeAccount()

SpecialCurrentAccount.java

/**
* pre condition: the balance >0
*                         the period > default period
* post condition: the account is closed
**/
public boolean closeAccount();

The pre-condition set in the sub-type SpecialCurrentAccount contains more conditions than the base-type CurrentAccount. This is against the Design by Contract principle. According to the Design by Contract:

A sub-type can only have weaker pre-conditions and stronger post-conditions than its base class.

Clearly, my previous design violated the Design by Contract principle.Having these facts in mind, I had a clear idea what was going wrong and approached a better LSP and Design by Contract compliant solution.

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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
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%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs