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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer 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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs